pci.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261
  1. /*
  2. * PCI Bus Services, see include/linux/pci.h for further explanation.
  3. *
  4. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  5. * David Mosberger-Tang
  6. *
  7. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/pm.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/string.h>
  18. #include <linux/log2.h>
  19. #include <linux/pci-aspm.h>
  20. #include <linux/pm_wakeup.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/pci_hotplug.h>
  25. #include <asm-generic/pci-bridge.h>
  26. #include <asm/setup.h>
  27. #include "pci.h"
  28. const char *pci_power_names[] = {
  29. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  30. };
  31. EXPORT_SYMBOL_GPL(pci_power_names);
  32. int isa_dma_bridge_buggy;
  33. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  34. int pci_pci_problems;
  35. EXPORT_SYMBOL(pci_pci_problems);
  36. unsigned int pci_pm_d3_delay;
  37. static void pci_pme_list_scan(struct work_struct *work);
  38. static LIST_HEAD(pci_pme_list);
  39. static DEFINE_MUTEX(pci_pme_list_mutex);
  40. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  41. struct pci_pme_device {
  42. struct list_head list;
  43. struct pci_dev *dev;
  44. };
  45. #define PME_TIMEOUT 1000 /* How long between PME checks */
  46. static void pci_dev_d3_sleep(struct pci_dev *dev)
  47. {
  48. unsigned int delay = dev->d3_delay;
  49. if (delay < pci_pm_d3_delay)
  50. delay = pci_pm_d3_delay;
  51. msleep(delay);
  52. }
  53. #ifdef CONFIG_PCI_DOMAINS
  54. int pci_domains_supported = 1;
  55. #endif
  56. #define DEFAULT_CARDBUS_IO_SIZE (256)
  57. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  58. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  59. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  60. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  61. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  62. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  63. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  64. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  65. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  66. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_TUNE_OFF;
  67. /*
  68. * The default CLS is used if arch didn't set CLS explicitly and not
  69. * all pci devices agree on the same value. Arch can override either
  70. * the dfl or actual value as it sees fit. Don't forget this is
  71. * measured in 32-bit words, not bytes.
  72. */
  73. u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
  74. u8 pci_cache_line_size;
  75. /*
  76. * If we set up a device for bus mastering, we need to check the latency
  77. * timer as certain BIOSes forget to set it properly.
  78. */
  79. unsigned int pcibios_max_latency = 255;
  80. /* If set, the PCIe ARI capability will not be used. */
  81. static bool pcie_ari_disabled;
  82. /**
  83. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  84. * @bus: pointer to PCI bus structure to search
  85. *
  86. * Given a PCI bus, returns the highest PCI bus number present in the set
  87. * including the given PCI bus and its list of child PCI buses.
  88. */
  89. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  90. {
  91. struct list_head *tmp;
  92. unsigned char max, n;
  93. max = bus->busn_res.end;
  94. list_for_each(tmp, &bus->children) {
  95. n = pci_bus_max_busnr(pci_bus_b(tmp));
  96. if(n > max)
  97. max = n;
  98. }
  99. return max;
  100. }
  101. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  102. #ifdef CONFIG_HAS_IOMEM
  103. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  104. {
  105. /*
  106. * Make sure the BAR is actually a memory resource, not an IO resource
  107. */
  108. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  109. WARN_ON(1);
  110. return NULL;
  111. }
  112. return ioremap_nocache(pci_resource_start(pdev, bar),
  113. pci_resource_len(pdev, bar));
  114. }
  115. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  116. #endif
  117. #define PCI_FIND_CAP_TTL 48
  118. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  119. u8 pos, int cap, int *ttl)
  120. {
  121. u8 id;
  122. while ((*ttl)--) {
  123. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  124. if (pos < 0x40)
  125. break;
  126. pos &= ~3;
  127. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  128. &id);
  129. if (id == 0xff)
  130. break;
  131. if (id == cap)
  132. return pos;
  133. pos += PCI_CAP_LIST_NEXT;
  134. }
  135. return 0;
  136. }
  137. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  138. u8 pos, int cap)
  139. {
  140. int ttl = PCI_FIND_CAP_TTL;
  141. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  142. }
  143. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  144. {
  145. return __pci_find_next_cap(dev->bus, dev->devfn,
  146. pos + PCI_CAP_LIST_NEXT, cap);
  147. }
  148. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  149. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  150. unsigned int devfn, u8 hdr_type)
  151. {
  152. u16 status;
  153. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  154. if (!(status & PCI_STATUS_CAP_LIST))
  155. return 0;
  156. switch (hdr_type) {
  157. case PCI_HEADER_TYPE_NORMAL:
  158. case PCI_HEADER_TYPE_BRIDGE:
  159. return PCI_CAPABILITY_LIST;
  160. case PCI_HEADER_TYPE_CARDBUS:
  161. return PCI_CB_CAPABILITY_LIST;
  162. default:
  163. return 0;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * pci_find_capability - query for devices' capabilities
  169. * @dev: PCI device to query
  170. * @cap: capability code
  171. *
  172. * Tell if a device supports a given PCI capability.
  173. * Returns the address of the requested capability structure within the
  174. * device's PCI configuration space or 0 in case the device does not
  175. * support it. Possible values for @cap:
  176. *
  177. * %PCI_CAP_ID_PM Power Management
  178. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  179. * %PCI_CAP_ID_VPD Vital Product Data
  180. * %PCI_CAP_ID_SLOTID Slot Identification
  181. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  182. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  183. * %PCI_CAP_ID_PCIX PCI-X
  184. * %PCI_CAP_ID_EXP PCI Express
  185. */
  186. int pci_find_capability(struct pci_dev *dev, int cap)
  187. {
  188. int pos;
  189. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  190. if (pos)
  191. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  192. return pos;
  193. }
  194. /**
  195. * pci_bus_find_capability - query for devices' capabilities
  196. * @bus: the PCI bus to query
  197. * @devfn: PCI device to query
  198. * @cap: capability code
  199. *
  200. * Like pci_find_capability() but works for pci devices that do not have a
  201. * pci_dev structure set up yet.
  202. *
  203. * Returns the address of the requested capability structure within the
  204. * device's PCI configuration space or 0 in case the device does not
  205. * support it.
  206. */
  207. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  208. {
  209. int pos;
  210. u8 hdr_type;
  211. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  212. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  213. if (pos)
  214. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  215. return pos;
  216. }
  217. /**
  218. * pci_find_next_ext_capability - Find an extended capability
  219. * @dev: PCI device to query
  220. * @start: address at which to start looking (0 to start at beginning of list)
  221. * @cap: capability code
  222. *
  223. * Returns the address of the next matching extended capability structure
  224. * within the device's PCI configuration space or 0 if the device does
  225. * not support it. Some capabilities can occur several times, e.g., the
  226. * vendor-specific capability, and this provides a way to find them all.
  227. */
  228. int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
  229. {
  230. u32 header;
  231. int ttl;
  232. int pos = PCI_CFG_SPACE_SIZE;
  233. /* minimum 8 bytes per capability */
  234. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  235. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  236. return 0;
  237. if (start)
  238. pos = start;
  239. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  240. return 0;
  241. /*
  242. * If we have no capabilities, this is indicated by cap ID,
  243. * cap version and next pointer all being 0.
  244. */
  245. if (header == 0)
  246. return 0;
  247. while (ttl-- > 0) {
  248. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  249. return pos;
  250. pos = PCI_EXT_CAP_NEXT(header);
  251. if (pos < PCI_CFG_SPACE_SIZE)
  252. break;
  253. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  254. break;
  255. }
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
  259. /**
  260. * pci_find_ext_capability - Find an extended capability
  261. * @dev: PCI device to query
  262. * @cap: capability code
  263. *
  264. * Returns the address of the requested extended capability structure
  265. * within the device's PCI configuration space or 0 if the device does
  266. * not support it. Possible values for @cap:
  267. *
  268. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  269. * %PCI_EXT_CAP_ID_VC Virtual Channel
  270. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  271. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  272. */
  273. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  274. {
  275. return pci_find_next_ext_capability(dev, 0, cap);
  276. }
  277. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  278. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  279. {
  280. int rc, ttl = PCI_FIND_CAP_TTL;
  281. u8 cap, mask;
  282. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  283. mask = HT_3BIT_CAP_MASK;
  284. else
  285. mask = HT_5BIT_CAP_MASK;
  286. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  287. PCI_CAP_ID_HT, &ttl);
  288. while (pos) {
  289. rc = pci_read_config_byte(dev, pos + 3, &cap);
  290. if (rc != PCIBIOS_SUCCESSFUL)
  291. return 0;
  292. if ((cap & mask) == ht_cap)
  293. return pos;
  294. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  295. pos + PCI_CAP_LIST_NEXT,
  296. PCI_CAP_ID_HT, &ttl);
  297. }
  298. return 0;
  299. }
  300. /**
  301. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  302. * @dev: PCI device to query
  303. * @pos: Position from which to continue searching
  304. * @ht_cap: Hypertransport capability code
  305. *
  306. * To be used in conjunction with pci_find_ht_capability() to search for
  307. * all capabilities matching @ht_cap. @pos should always be a value returned
  308. * from pci_find_ht_capability().
  309. *
  310. * NB. To be 100% safe against broken PCI devices, the caller should take
  311. * steps to avoid an infinite loop.
  312. */
  313. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  314. {
  315. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  316. }
  317. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  318. /**
  319. * pci_find_ht_capability - query a device's Hypertransport capabilities
  320. * @dev: PCI device to query
  321. * @ht_cap: Hypertransport capability code
  322. *
  323. * Tell if a device supports a given Hypertransport capability.
  324. * Returns an address within the device's PCI configuration space
  325. * or 0 in case the device does not support the request capability.
  326. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  327. * which has a Hypertransport capability matching @ht_cap.
  328. */
  329. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  330. {
  331. int pos;
  332. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  333. if (pos)
  334. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  335. return pos;
  336. }
  337. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  338. /**
  339. * pci_find_parent_resource - return resource region of parent bus of given region
  340. * @dev: PCI device structure contains resources to be searched
  341. * @res: child resource record for which parent is sought
  342. *
  343. * For given resource region of given device, return the resource
  344. * region of parent bus the given region is contained in or where
  345. * it should be allocated from.
  346. */
  347. struct resource *
  348. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  349. {
  350. const struct pci_bus *bus = dev->bus;
  351. int i;
  352. struct resource *best = NULL, *r;
  353. pci_bus_for_each_resource(bus, r, i) {
  354. if (!r)
  355. continue;
  356. if (res->start && !(res->start >= r->start && res->end <= r->end))
  357. continue; /* Not contained */
  358. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  359. continue; /* Wrong type */
  360. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  361. return r; /* Exact match */
  362. /* We can't insert a non-prefetch resource inside a prefetchable parent .. */
  363. if (r->flags & IORESOURCE_PREFETCH)
  364. continue;
  365. /* .. but we can put a prefetchable resource inside a non-prefetchable one */
  366. if (!best)
  367. best = r;
  368. }
  369. return best;
  370. }
  371. /**
  372. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  373. * @dev: PCI device to have its BARs restored
  374. *
  375. * Restore the BAR values for a given device, so as to make it
  376. * accessible by its driver.
  377. */
  378. static void
  379. pci_restore_bars(struct pci_dev *dev)
  380. {
  381. int i;
  382. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  383. pci_update_resource(dev, i);
  384. }
  385. static struct pci_platform_pm_ops *pci_platform_pm;
  386. int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
  387. {
  388. if (!ops->is_manageable || !ops->set_state || !ops->choose_state
  389. || !ops->sleep_wake)
  390. return -EINVAL;
  391. pci_platform_pm = ops;
  392. return 0;
  393. }
  394. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  395. {
  396. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  397. }
  398. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  399. pci_power_t t)
  400. {
  401. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  402. }
  403. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  404. {
  405. return pci_platform_pm ?
  406. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  407. }
  408. static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
  409. {
  410. return pci_platform_pm ?
  411. pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
  412. }
  413. static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable)
  414. {
  415. return pci_platform_pm ?
  416. pci_platform_pm->run_wake(dev, enable) : -ENODEV;
  417. }
  418. /**
  419. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  420. * given PCI device
  421. * @dev: PCI device to handle.
  422. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  423. *
  424. * RETURN VALUE:
  425. * -EINVAL if the requested state is invalid.
  426. * -EIO if device does not support PCI PM or its PM capabilities register has a
  427. * wrong version, or device doesn't support the requested state.
  428. * 0 if device already is in the requested state.
  429. * 0 if device's power state has been successfully changed.
  430. */
  431. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  432. {
  433. u16 pmcsr;
  434. bool need_restore = false;
  435. /* Check if we're already there */
  436. if (dev->current_state == state)
  437. return 0;
  438. if (!dev->pm_cap)
  439. return -EIO;
  440. if (state < PCI_D0 || state > PCI_D3hot)
  441. return -EINVAL;
  442. /* Validate current state:
  443. * Can enter D0 from any state, but if we can only go deeper
  444. * to sleep if we're already in a low power state
  445. */
  446. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  447. && dev->current_state > state) {
  448. dev_err(&dev->dev, "invalid power transition "
  449. "(from state %d to %d)\n", dev->current_state, state);
  450. return -EINVAL;
  451. }
  452. /* check if this device supports the desired state */
  453. if ((state == PCI_D1 && !dev->d1_support)
  454. || (state == PCI_D2 && !dev->d2_support))
  455. return -EIO;
  456. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  457. /* If we're (effectively) in D3, force entire word to 0.
  458. * This doesn't affect PME_Status, disables PME_En, and
  459. * sets PowerState to 0.
  460. */
  461. switch (dev->current_state) {
  462. case PCI_D0:
  463. case PCI_D1:
  464. case PCI_D2:
  465. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  466. pmcsr |= state;
  467. break;
  468. case PCI_D3hot:
  469. case PCI_D3cold:
  470. case PCI_UNKNOWN: /* Boot-up */
  471. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  472. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  473. need_restore = true;
  474. /* Fall-through: force to D0 */
  475. default:
  476. pmcsr = 0;
  477. break;
  478. }
  479. /* enter specified state */
  480. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  481. /* Mandatory power management transition delays */
  482. /* see PCI PM 1.1 5.6.1 table 18 */
  483. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  484. pci_dev_d3_sleep(dev);
  485. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  486. udelay(PCI_PM_D2_DELAY);
  487. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  488. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  489. if (dev->current_state != state && printk_ratelimit())
  490. dev_info(&dev->dev, "Refused to change power state, "
  491. "currently in D%d\n", dev->current_state);
  492. /*
  493. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  494. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  495. * from D3hot to D0 _may_ perform an internal reset, thereby
  496. * going to "D0 Uninitialized" rather than "D0 Initialized".
  497. * For example, at least some versions of the 3c905B and the
  498. * 3c556B exhibit this behaviour.
  499. *
  500. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  501. * devices in a D3hot state at boot. Consequently, we need to
  502. * restore at least the BARs so that the device will be
  503. * accessible to its driver.
  504. */
  505. if (need_restore)
  506. pci_restore_bars(dev);
  507. if (dev->bus->self)
  508. pcie_aspm_pm_state_change(dev->bus->self);
  509. return 0;
  510. }
  511. /**
  512. * pci_update_current_state - Read PCI power state of given device from its
  513. * PCI PM registers and cache it
  514. * @dev: PCI device to handle.
  515. * @state: State to cache in case the device doesn't have the PM capability
  516. */
  517. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  518. {
  519. if (dev->pm_cap) {
  520. u16 pmcsr;
  521. /*
  522. * Configuration space is not accessible for device in
  523. * D3cold, so just keep or set D3cold for safety
  524. */
  525. if (dev->current_state == PCI_D3cold)
  526. return;
  527. if (state == PCI_D3cold) {
  528. dev->current_state = PCI_D3cold;
  529. return;
  530. }
  531. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  532. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  533. } else {
  534. dev->current_state = state;
  535. }
  536. }
  537. /**
  538. * pci_power_up - Put the given device into D0 forcibly
  539. * @dev: PCI device to power up
  540. */
  541. void pci_power_up(struct pci_dev *dev)
  542. {
  543. if (platform_pci_power_manageable(dev))
  544. platform_pci_set_power_state(dev, PCI_D0);
  545. pci_raw_set_power_state(dev, PCI_D0);
  546. pci_update_current_state(dev, PCI_D0);
  547. }
  548. /**
  549. * pci_platform_power_transition - Use platform to change device power state
  550. * @dev: PCI device to handle.
  551. * @state: State to put the device into.
  552. */
  553. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  554. {
  555. int error;
  556. if (platform_pci_power_manageable(dev)) {
  557. error = platform_pci_set_power_state(dev, state);
  558. if (!error)
  559. pci_update_current_state(dev, state);
  560. } else
  561. error = -ENODEV;
  562. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  563. dev->current_state = PCI_D0;
  564. return error;
  565. }
  566. /**
  567. * __pci_start_power_transition - Start power transition of a PCI device
  568. * @dev: PCI device to handle.
  569. * @state: State to put the device into.
  570. */
  571. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  572. {
  573. if (state == PCI_D0) {
  574. pci_platform_power_transition(dev, PCI_D0);
  575. /*
  576. * Mandatory power management transition delays, see
  577. * PCI Express Base Specification Revision 2.0 Section
  578. * 6.6.1: Conventional Reset. Do not delay for
  579. * devices powered on/off by corresponding bridge,
  580. * because have already delayed for the bridge.
  581. */
  582. if (dev->runtime_d3cold) {
  583. msleep(dev->d3cold_delay);
  584. /*
  585. * When powering on a bridge from D3cold, the
  586. * whole hierarchy may be powered on into
  587. * D0uninitialized state, resume them to give
  588. * them a chance to suspend again
  589. */
  590. pci_wakeup_bus(dev->subordinate);
  591. }
  592. }
  593. }
  594. /**
  595. * __pci_dev_set_current_state - Set current state of a PCI device
  596. * @dev: Device to handle
  597. * @data: pointer to state to be set
  598. */
  599. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  600. {
  601. pci_power_t state = *(pci_power_t *)data;
  602. dev->current_state = state;
  603. return 0;
  604. }
  605. /**
  606. * __pci_bus_set_current_state - Walk given bus and set current state of devices
  607. * @bus: Top bus of the subtree to walk.
  608. * @state: state to be set
  609. */
  610. static void __pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  611. {
  612. if (bus)
  613. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  614. }
  615. /**
  616. * __pci_complete_power_transition - Complete power transition of a PCI device
  617. * @dev: PCI device to handle.
  618. * @state: State to put the device into.
  619. *
  620. * This function should not be called directly by device drivers.
  621. */
  622. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  623. {
  624. int ret;
  625. if (state <= PCI_D0)
  626. return -EINVAL;
  627. ret = pci_platform_power_transition(dev, state);
  628. /* Power off the bridge may power off the whole hierarchy */
  629. if (!ret && state == PCI_D3cold)
  630. __pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  631. return ret;
  632. }
  633. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  634. /**
  635. * pci_set_power_state - Set the power state of a PCI device
  636. * @dev: PCI device to handle.
  637. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  638. *
  639. * Transition a device to a new power state, using the platform firmware and/or
  640. * the device's PCI PM registers.
  641. *
  642. * RETURN VALUE:
  643. * -EINVAL if the requested state is invalid.
  644. * -EIO if device does not support PCI PM or its PM capabilities register has a
  645. * wrong version, or device doesn't support the requested state.
  646. * 0 if device already is in the requested state.
  647. * 0 if device's power state has been successfully changed.
  648. */
  649. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  650. {
  651. int error;
  652. /* bound the state we're entering */
  653. if (state > PCI_D3cold)
  654. state = PCI_D3cold;
  655. else if (state < PCI_D0)
  656. state = PCI_D0;
  657. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  658. /*
  659. * If the device or the parent bridge do not support PCI PM,
  660. * ignore the request if we're doing anything other than putting
  661. * it into D0 (which would only happen on boot).
  662. */
  663. return 0;
  664. /* Check if we're already there */
  665. if (dev->current_state == state)
  666. return 0;
  667. __pci_start_power_transition(dev, state);
  668. /* This device is quirked not to be put into D3, so
  669. don't put it in D3 */
  670. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  671. return 0;
  672. /*
  673. * To put device in D3cold, we put device into D3hot in native
  674. * way, then put device into D3cold with platform ops
  675. */
  676. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  677. PCI_D3hot : state);
  678. if (!__pci_complete_power_transition(dev, state))
  679. error = 0;
  680. /*
  681. * When aspm_policy is "powersave" this call ensures
  682. * that ASPM is configured.
  683. */
  684. if (!error && dev->bus->self)
  685. pcie_aspm_powersave_config_link(dev->bus->self);
  686. return error;
  687. }
  688. /**
  689. * pci_choose_state - Choose the power state of a PCI device
  690. * @dev: PCI device to be suspended
  691. * @state: target sleep state for the whole system. This is the value
  692. * that is passed to suspend() function.
  693. *
  694. * Returns PCI power state suitable for given device and given system
  695. * message.
  696. */
  697. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  698. {
  699. pci_power_t ret;
  700. if (!dev->pm_cap)
  701. return PCI_D0;
  702. ret = platform_pci_choose_state(dev);
  703. if (ret != PCI_POWER_ERROR)
  704. return ret;
  705. switch (state.event) {
  706. case PM_EVENT_ON:
  707. return PCI_D0;
  708. case PM_EVENT_FREEZE:
  709. case PM_EVENT_PRETHAW:
  710. /* REVISIT both freeze and pre-thaw "should" use D0 */
  711. case PM_EVENT_SUSPEND:
  712. case PM_EVENT_HIBERNATE:
  713. return PCI_D3hot;
  714. default:
  715. dev_info(&dev->dev, "unrecognized suspend event %d\n",
  716. state.event);
  717. BUG();
  718. }
  719. return PCI_D0;
  720. }
  721. EXPORT_SYMBOL(pci_choose_state);
  722. #define PCI_EXP_SAVE_REGS 7
  723. static struct pci_cap_saved_state *pci_find_saved_cap(
  724. struct pci_dev *pci_dev, char cap)
  725. {
  726. struct pci_cap_saved_state *tmp;
  727. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  728. if (tmp->cap.cap_nr == cap)
  729. return tmp;
  730. }
  731. return NULL;
  732. }
  733. static int pci_save_pcie_state(struct pci_dev *dev)
  734. {
  735. int i = 0;
  736. struct pci_cap_saved_state *save_state;
  737. u16 *cap;
  738. if (!pci_is_pcie(dev))
  739. return 0;
  740. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  741. if (!save_state) {
  742. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  743. return -ENOMEM;
  744. }
  745. cap = (u16 *)&save_state->cap.data[0];
  746. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  747. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  748. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  749. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  750. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  751. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  752. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  753. return 0;
  754. }
  755. static void pci_restore_pcie_state(struct pci_dev *dev)
  756. {
  757. int i = 0;
  758. struct pci_cap_saved_state *save_state;
  759. u16 *cap;
  760. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  761. if (!save_state)
  762. return;
  763. cap = (u16 *)&save_state->cap.data[0];
  764. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  765. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  766. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  767. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  768. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  769. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  770. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  771. }
  772. static int pci_save_pcix_state(struct pci_dev *dev)
  773. {
  774. int pos;
  775. struct pci_cap_saved_state *save_state;
  776. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  777. if (pos <= 0)
  778. return 0;
  779. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  780. if (!save_state) {
  781. dev_err(&dev->dev, "buffer not found in %s\n", __func__);
  782. return -ENOMEM;
  783. }
  784. pci_read_config_word(dev, pos + PCI_X_CMD,
  785. (u16 *)save_state->cap.data);
  786. return 0;
  787. }
  788. static void pci_restore_pcix_state(struct pci_dev *dev)
  789. {
  790. int i = 0, pos;
  791. struct pci_cap_saved_state *save_state;
  792. u16 *cap;
  793. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  794. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  795. if (!save_state || pos <= 0)
  796. return;
  797. cap = (u16 *)&save_state->cap.data[0];
  798. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  799. }
  800. /**
  801. * pci_save_state - save the PCI configuration space of a device before suspending
  802. * @dev: - PCI device that we're dealing with
  803. */
  804. int
  805. pci_save_state(struct pci_dev *dev)
  806. {
  807. int i;
  808. /* XXX: 100% dword access ok here? */
  809. for (i = 0; i < 16; i++)
  810. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  811. dev->state_saved = true;
  812. if ((i = pci_save_pcie_state(dev)) != 0)
  813. return i;
  814. if ((i = pci_save_pcix_state(dev)) != 0)
  815. return i;
  816. return 0;
  817. }
  818. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  819. u32 saved_val, int retry)
  820. {
  821. u32 val;
  822. pci_read_config_dword(pdev, offset, &val);
  823. if (val == saved_val)
  824. return;
  825. for (;;) {
  826. dev_dbg(&pdev->dev, "restoring config space at offset "
  827. "%#x (was %#x, writing %#x)\n", offset, val, saved_val);
  828. pci_write_config_dword(pdev, offset, saved_val);
  829. if (retry-- <= 0)
  830. return;
  831. pci_read_config_dword(pdev, offset, &val);
  832. if (val == saved_val)
  833. return;
  834. mdelay(1);
  835. }
  836. }
  837. static void pci_restore_config_space_range(struct pci_dev *pdev,
  838. int start, int end, int retry)
  839. {
  840. int index;
  841. for (index = end; index >= start; index--)
  842. pci_restore_config_dword(pdev, 4 * index,
  843. pdev->saved_config_space[index],
  844. retry);
  845. }
  846. static void pci_restore_config_space(struct pci_dev *pdev)
  847. {
  848. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  849. pci_restore_config_space_range(pdev, 10, 15, 0);
  850. /* Restore BARs before the command register. */
  851. pci_restore_config_space_range(pdev, 4, 9, 10);
  852. pci_restore_config_space_range(pdev, 0, 3, 0);
  853. } else {
  854. pci_restore_config_space_range(pdev, 0, 15, 0);
  855. }
  856. }
  857. /**
  858. * pci_restore_state - Restore the saved state of a PCI device
  859. * @dev: - PCI device that we're dealing with
  860. */
  861. void pci_restore_state(struct pci_dev *dev)
  862. {
  863. if (!dev->state_saved)
  864. return;
  865. /* PCI Express register must be restored first */
  866. pci_restore_pcie_state(dev);
  867. pci_restore_ats_state(dev);
  868. pci_restore_config_space(dev);
  869. pci_restore_pcix_state(dev);
  870. pci_restore_msi_state(dev);
  871. pci_restore_iov_state(dev);
  872. dev->state_saved = false;
  873. }
  874. struct pci_saved_state {
  875. u32 config_space[16];
  876. struct pci_cap_saved_data cap[0];
  877. };
  878. /**
  879. * pci_store_saved_state - Allocate and return an opaque struct containing
  880. * the device saved state.
  881. * @dev: PCI device that we're dealing with
  882. *
  883. * Rerturn NULL if no state or error.
  884. */
  885. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  886. {
  887. struct pci_saved_state *state;
  888. struct pci_cap_saved_state *tmp;
  889. struct pci_cap_saved_data *cap;
  890. size_t size;
  891. if (!dev->state_saved)
  892. return NULL;
  893. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  894. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  895. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  896. state = kzalloc(size, GFP_KERNEL);
  897. if (!state)
  898. return NULL;
  899. memcpy(state->config_space, dev->saved_config_space,
  900. sizeof(state->config_space));
  901. cap = state->cap;
  902. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  903. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  904. memcpy(cap, &tmp->cap, len);
  905. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  906. }
  907. /* Empty cap_save terminates list */
  908. return state;
  909. }
  910. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  911. /**
  912. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  913. * @dev: PCI device that we're dealing with
  914. * @state: Saved state returned from pci_store_saved_state()
  915. */
  916. int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state)
  917. {
  918. struct pci_cap_saved_data *cap;
  919. dev->state_saved = false;
  920. if (!state)
  921. return 0;
  922. memcpy(dev->saved_config_space, state->config_space,
  923. sizeof(state->config_space));
  924. cap = state->cap;
  925. while (cap->size) {
  926. struct pci_cap_saved_state *tmp;
  927. tmp = pci_find_saved_cap(dev, cap->cap_nr);
  928. if (!tmp || tmp->cap.size != cap->size)
  929. return -EINVAL;
  930. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  931. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  932. sizeof(struct pci_cap_saved_data) + cap->size);
  933. }
  934. dev->state_saved = true;
  935. return 0;
  936. }
  937. EXPORT_SYMBOL_GPL(pci_load_saved_state);
  938. /**
  939. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  940. * and free the memory allocated for it.
  941. * @dev: PCI device that we're dealing with
  942. * @state: Pointer to saved state returned from pci_store_saved_state()
  943. */
  944. int pci_load_and_free_saved_state(struct pci_dev *dev,
  945. struct pci_saved_state **state)
  946. {
  947. int ret = pci_load_saved_state(dev, *state);
  948. kfree(*state);
  949. *state = NULL;
  950. return ret;
  951. }
  952. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  953. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  954. {
  955. int err;
  956. err = pci_set_power_state(dev, PCI_D0);
  957. if (err < 0 && err != -EIO)
  958. return err;
  959. err = pcibios_enable_device(dev, bars);
  960. if (err < 0)
  961. return err;
  962. pci_fixup_device(pci_fixup_enable, dev);
  963. return 0;
  964. }
  965. /**
  966. * pci_reenable_device - Resume abandoned device
  967. * @dev: PCI device to be resumed
  968. *
  969. * Note this function is a backend of pci_default_resume and is not supposed
  970. * to be called by normal code, write proper resume handler and use it instead.
  971. */
  972. int pci_reenable_device(struct pci_dev *dev)
  973. {
  974. if (pci_is_enabled(dev))
  975. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  976. return 0;
  977. }
  978. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  979. {
  980. int err;
  981. int i, bars = 0;
  982. /*
  983. * Power state could be unknown at this point, either due to a fresh
  984. * boot or a device removal call. So get the current power state
  985. * so that things like MSI message writing will behave as expected
  986. * (e.g. if the device really is in D0 at enable time).
  987. */
  988. if (dev->pm_cap) {
  989. u16 pmcsr;
  990. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  991. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  992. }
  993. if (atomic_inc_return(&dev->enable_cnt) > 1)
  994. return 0; /* already enabled */
  995. /* only skip sriov related */
  996. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  997. if (dev->resource[i].flags & flags)
  998. bars |= (1 << i);
  999. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1000. if (dev->resource[i].flags & flags)
  1001. bars |= (1 << i);
  1002. err = do_pci_enable_device(dev, bars);
  1003. if (err < 0)
  1004. atomic_dec(&dev->enable_cnt);
  1005. return err;
  1006. }
  1007. /**
  1008. * pci_enable_device_io - Initialize a device for use with IO space
  1009. * @dev: PCI device to be initialized
  1010. *
  1011. * Initialize device before it's used by a driver. Ask low-level code
  1012. * to enable I/O resources. Wake up the device if it was suspended.
  1013. * Beware, this function can fail.
  1014. */
  1015. int pci_enable_device_io(struct pci_dev *dev)
  1016. {
  1017. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1018. }
  1019. /**
  1020. * pci_enable_device_mem - Initialize a device for use with Memory space
  1021. * @dev: PCI device to be initialized
  1022. *
  1023. * Initialize device before it's used by a driver. Ask low-level code
  1024. * to enable Memory resources. Wake up the device if it was suspended.
  1025. * Beware, this function can fail.
  1026. */
  1027. int pci_enable_device_mem(struct pci_dev *dev)
  1028. {
  1029. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1030. }
  1031. /**
  1032. * pci_enable_device - Initialize device before it's used by a driver.
  1033. * @dev: PCI device to be initialized
  1034. *
  1035. * Initialize device before it's used by a driver. Ask low-level code
  1036. * to enable I/O and memory. Wake up the device if it was suspended.
  1037. * Beware, this function can fail.
  1038. *
  1039. * Note we don't actually enable the device many times if we call
  1040. * this function repeatedly (we just increment the count).
  1041. */
  1042. int pci_enable_device(struct pci_dev *dev)
  1043. {
  1044. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1045. }
  1046. /*
  1047. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1048. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1049. * there's no need to track it separately. pci_devres is initialized
  1050. * when a device is enabled using managed PCI device enable interface.
  1051. */
  1052. struct pci_devres {
  1053. unsigned int enabled:1;
  1054. unsigned int pinned:1;
  1055. unsigned int orig_intx:1;
  1056. unsigned int restore_intx:1;
  1057. u32 region_mask;
  1058. };
  1059. static void pcim_release(struct device *gendev, void *res)
  1060. {
  1061. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  1062. struct pci_devres *this = res;
  1063. int i;
  1064. if (dev->msi_enabled)
  1065. pci_disable_msi(dev);
  1066. if (dev->msix_enabled)
  1067. pci_disable_msix(dev);
  1068. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1069. if (this->region_mask & (1 << i))
  1070. pci_release_region(dev, i);
  1071. if (this->restore_intx)
  1072. pci_intx(dev, this->orig_intx);
  1073. if (this->enabled && !this->pinned)
  1074. pci_disable_device(dev);
  1075. }
  1076. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  1077. {
  1078. struct pci_devres *dr, *new_dr;
  1079. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1080. if (dr)
  1081. return dr;
  1082. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1083. if (!new_dr)
  1084. return NULL;
  1085. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1086. }
  1087. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  1088. {
  1089. if (pci_is_managed(pdev))
  1090. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1091. return NULL;
  1092. }
  1093. /**
  1094. * pcim_enable_device - Managed pci_enable_device()
  1095. * @pdev: PCI device to be initialized
  1096. *
  1097. * Managed pci_enable_device().
  1098. */
  1099. int pcim_enable_device(struct pci_dev *pdev)
  1100. {
  1101. struct pci_devres *dr;
  1102. int rc;
  1103. dr = get_pci_dr(pdev);
  1104. if (unlikely(!dr))
  1105. return -ENOMEM;
  1106. if (dr->enabled)
  1107. return 0;
  1108. rc = pci_enable_device(pdev);
  1109. if (!rc) {
  1110. pdev->is_managed = 1;
  1111. dr->enabled = 1;
  1112. }
  1113. return rc;
  1114. }
  1115. /**
  1116. * pcim_pin_device - Pin managed PCI device
  1117. * @pdev: PCI device to pin
  1118. *
  1119. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1120. * driver detach. @pdev must have been enabled with
  1121. * pcim_enable_device().
  1122. */
  1123. void pcim_pin_device(struct pci_dev *pdev)
  1124. {
  1125. struct pci_devres *dr;
  1126. dr = find_pci_dr(pdev);
  1127. WARN_ON(!dr || !dr->enabled);
  1128. if (dr)
  1129. dr->pinned = 1;
  1130. }
  1131. /*
  1132. * pcibios_add_device - provide arch specific hooks when adding device dev
  1133. * @dev: the PCI device being added
  1134. *
  1135. * Permits the platform to provide architecture specific functionality when
  1136. * devices are added. This is the default implementation. Architecture
  1137. * implementations can override this.
  1138. */
  1139. int __weak pcibios_add_device (struct pci_dev *dev)
  1140. {
  1141. return 0;
  1142. }
  1143. /**
  1144. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1145. * @dev: the PCI device being released
  1146. *
  1147. * Permits the platform to provide architecture specific functionality when
  1148. * devices are released. This is the default implementation. Architecture
  1149. * implementations can override this.
  1150. */
  1151. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1152. /**
  1153. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1154. * @dev: the PCI device to disable
  1155. *
  1156. * Disables architecture specific PCI resources for the device. This
  1157. * is the default implementation. Architecture implementations can
  1158. * override this.
  1159. */
  1160. void __weak pcibios_disable_device (struct pci_dev *dev) {}
  1161. static void do_pci_disable_device(struct pci_dev *dev)
  1162. {
  1163. u16 pci_command;
  1164. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1165. if (pci_command & PCI_COMMAND_MASTER) {
  1166. pci_command &= ~PCI_COMMAND_MASTER;
  1167. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1168. }
  1169. pcibios_disable_device(dev);
  1170. }
  1171. /**
  1172. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1173. * @dev: PCI device to disable
  1174. *
  1175. * NOTE: This function is a backend of PCI power management routines and is
  1176. * not supposed to be called drivers.
  1177. */
  1178. void pci_disable_enabled_device(struct pci_dev *dev)
  1179. {
  1180. if (pci_is_enabled(dev))
  1181. do_pci_disable_device(dev);
  1182. }
  1183. /**
  1184. * pci_disable_device - Disable PCI device after use
  1185. * @dev: PCI device to be disabled
  1186. *
  1187. * Signal to the system that the PCI device is not in use by the system
  1188. * anymore. This only involves disabling PCI bus-mastering, if active.
  1189. *
  1190. * Note we don't actually disable the device until all callers of
  1191. * pci_enable_device() have called pci_disable_device().
  1192. */
  1193. void
  1194. pci_disable_device(struct pci_dev *dev)
  1195. {
  1196. struct pci_devres *dr;
  1197. dr = find_pci_dr(dev);
  1198. if (dr)
  1199. dr->enabled = 0;
  1200. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1201. "disabling already-disabled device");
  1202. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1203. return;
  1204. do_pci_disable_device(dev);
  1205. dev->is_busmaster = 0;
  1206. }
  1207. /**
  1208. * pcibios_set_pcie_reset_state - set reset state for device dev
  1209. * @dev: the PCIe device reset
  1210. * @state: Reset state to enter into
  1211. *
  1212. *
  1213. * Sets the PCIe reset state for the device. This is the default
  1214. * implementation. Architecture implementations can override this.
  1215. */
  1216. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1217. enum pcie_reset_state state)
  1218. {
  1219. return -EINVAL;
  1220. }
  1221. /**
  1222. * pci_set_pcie_reset_state - set reset state for device dev
  1223. * @dev: the PCIe device reset
  1224. * @state: Reset state to enter into
  1225. *
  1226. *
  1227. * Sets the PCI reset state for the device.
  1228. */
  1229. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1230. {
  1231. return pcibios_set_pcie_reset_state(dev, state);
  1232. }
  1233. /**
  1234. * pci_check_pme_status - Check if given device has generated PME.
  1235. * @dev: Device to check.
  1236. *
  1237. * Check the PME status of the device and if set, clear it and clear PME enable
  1238. * (if set). Return 'true' if PME status and PME enable were both set or
  1239. * 'false' otherwise.
  1240. */
  1241. bool pci_check_pme_status(struct pci_dev *dev)
  1242. {
  1243. int pmcsr_pos;
  1244. u16 pmcsr;
  1245. bool ret = false;
  1246. if (!dev->pm_cap)
  1247. return false;
  1248. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1249. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1250. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1251. return false;
  1252. /* Clear PME status. */
  1253. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1254. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1255. /* Disable PME to avoid interrupt flood. */
  1256. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1257. ret = true;
  1258. }
  1259. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1260. return ret;
  1261. }
  1262. /**
  1263. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1264. * @dev: Device to handle.
  1265. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1266. *
  1267. * Check if @dev has generated PME and queue a resume request for it in that
  1268. * case.
  1269. */
  1270. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1271. {
  1272. if (pme_poll_reset && dev->pme_poll)
  1273. dev->pme_poll = false;
  1274. if (pci_check_pme_status(dev)) {
  1275. pci_wakeup_event(dev);
  1276. pm_request_resume(&dev->dev);
  1277. }
  1278. return 0;
  1279. }
  1280. /**
  1281. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1282. * @bus: Top bus of the subtree to walk.
  1283. */
  1284. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1285. {
  1286. if (bus)
  1287. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1288. }
  1289. /**
  1290. * pci_wakeup - Wake up a PCI device
  1291. * @pci_dev: Device to handle.
  1292. * @ign: ignored parameter
  1293. */
  1294. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  1295. {
  1296. pci_wakeup_event(pci_dev);
  1297. pm_request_resume(&pci_dev->dev);
  1298. return 0;
  1299. }
  1300. /**
  1301. * pci_wakeup_bus - Walk given bus and wake up devices on it
  1302. * @bus: Top bus of the subtree to walk.
  1303. */
  1304. void pci_wakeup_bus(struct pci_bus *bus)
  1305. {
  1306. if (bus)
  1307. pci_walk_bus(bus, pci_wakeup, NULL);
  1308. }
  1309. /**
  1310. * pci_pme_capable - check the capability of PCI device to generate PME#
  1311. * @dev: PCI device to handle.
  1312. * @state: PCI state from which device will issue PME#.
  1313. */
  1314. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1315. {
  1316. if (!dev->pm_cap)
  1317. return false;
  1318. return !!(dev->pme_support & (1 << state));
  1319. }
  1320. static void pci_pme_list_scan(struct work_struct *work)
  1321. {
  1322. struct pci_pme_device *pme_dev, *n;
  1323. mutex_lock(&pci_pme_list_mutex);
  1324. if (!list_empty(&pci_pme_list)) {
  1325. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1326. if (pme_dev->dev->pme_poll) {
  1327. struct pci_dev *bridge;
  1328. bridge = pme_dev->dev->bus->self;
  1329. /*
  1330. * If bridge is in low power state, the
  1331. * configuration space of subordinate devices
  1332. * may be not accessible
  1333. */
  1334. if (bridge && bridge->current_state != PCI_D0)
  1335. continue;
  1336. pci_pme_wakeup(pme_dev->dev, NULL);
  1337. } else {
  1338. list_del(&pme_dev->list);
  1339. kfree(pme_dev);
  1340. }
  1341. }
  1342. if (!list_empty(&pci_pme_list))
  1343. schedule_delayed_work(&pci_pme_work,
  1344. msecs_to_jiffies(PME_TIMEOUT));
  1345. }
  1346. mutex_unlock(&pci_pme_list_mutex);
  1347. }
  1348. /**
  1349. * pci_pme_active - enable or disable PCI device's PME# function
  1350. * @dev: PCI device to handle.
  1351. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1352. *
  1353. * The caller must verify that the device is capable of generating PME# before
  1354. * calling this function with @enable equal to 'true'.
  1355. */
  1356. void pci_pme_active(struct pci_dev *dev, bool enable)
  1357. {
  1358. u16 pmcsr;
  1359. if (!dev->pme_support)
  1360. return;
  1361. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1362. /* Clear PME_Status by writing 1 to it and enable PME# */
  1363. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1364. if (!enable)
  1365. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1366. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1367. /*
  1368. * PCI (as opposed to PCIe) PME requires that the device have
  1369. * its PME# line hooked up correctly. Not all hardware vendors
  1370. * do this, so the PME never gets delivered and the device
  1371. * remains asleep. The easiest way around this is to
  1372. * periodically walk the list of suspended devices and check
  1373. * whether any have their PME flag set. The assumption is that
  1374. * we'll wake up often enough anyway that this won't be a huge
  1375. * hit, and the power savings from the devices will still be a
  1376. * win.
  1377. *
  1378. * Although PCIe uses in-band PME message instead of PME# line
  1379. * to report PME, PME does not work for some PCIe devices in
  1380. * reality. For example, there are devices that set their PME
  1381. * status bits, but don't really bother to send a PME message;
  1382. * there are PCI Express Root Ports that don't bother to
  1383. * trigger interrupts when they receive PME messages from the
  1384. * devices below. So PME poll is used for PCIe devices too.
  1385. */
  1386. if (dev->pme_poll) {
  1387. struct pci_pme_device *pme_dev;
  1388. if (enable) {
  1389. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1390. GFP_KERNEL);
  1391. if (!pme_dev)
  1392. goto out;
  1393. pme_dev->dev = dev;
  1394. mutex_lock(&pci_pme_list_mutex);
  1395. list_add(&pme_dev->list, &pci_pme_list);
  1396. if (list_is_singular(&pci_pme_list))
  1397. schedule_delayed_work(&pci_pme_work,
  1398. msecs_to_jiffies(PME_TIMEOUT));
  1399. mutex_unlock(&pci_pme_list_mutex);
  1400. } else {
  1401. mutex_lock(&pci_pme_list_mutex);
  1402. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1403. if (pme_dev->dev == dev) {
  1404. list_del(&pme_dev->list);
  1405. kfree(pme_dev);
  1406. break;
  1407. }
  1408. }
  1409. mutex_unlock(&pci_pme_list_mutex);
  1410. }
  1411. }
  1412. out:
  1413. dev_dbg(&dev->dev, "PME# %s\n", enable ? "enabled" : "disabled");
  1414. }
  1415. /**
  1416. * __pci_enable_wake - enable PCI device as wakeup event source
  1417. * @dev: PCI device affected
  1418. * @state: PCI state from which device will issue wakeup events
  1419. * @runtime: True if the events are to be generated at run time
  1420. * @enable: True to enable event generation; false to disable
  1421. *
  1422. * This enables the device as a wakeup event source, or disables it.
  1423. * When such events involves platform-specific hooks, those hooks are
  1424. * called automatically by this routine.
  1425. *
  1426. * Devices with legacy power management (no standard PCI PM capabilities)
  1427. * always require such platform hooks.
  1428. *
  1429. * RETURN VALUE:
  1430. * 0 is returned on success
  1431. * -EINVAL is returned if device is not supposed to wake up the system
  1432. * Error code depending on the platform is returned if both the platform and
  1433. * the native mechanism fail to enable the generation of wake-up events
  1434. */
  1435. int __pci_enable_wake(struct pci_dev *dev, pci_power_t state,
  1436. bool runtime, bool enable)
  1437. {
  1438. int ret = 0;
  1439. if (enable && !runtime && !device_may_wakeup(&dev->dev))
  1440. return -EINVAL;
  1441. /* Don't do the same thing twice in a row for one device. */
  1442. if (!!enable == !!dev->wakeup_prepared)
  1443. return 0;
  1444. /*
  1445. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1446. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1447. * enable. To disable wake-up we call the platform first, for symmetry.
  1448. */
  1449. if (enable) {
  1450. int error;
  1451. if (pci_pme_capable(dev, state))
  1452. pci_pme_active(dev, true);
  1453. else
  1454. ret = 1;
  1455. error = runtime ? platform_pci_run_wake(dev, true) :
  1456. platform_pci_sleep_wake(dev, true);
  1457. if (ret)
  1458. ret = error;
  1459. if (!ret)
  1460. dev->wakeup_prepared = true;
  1461. } else {
  1462. if (runtime)
  1463. platform_pci_run_wake(dev, false);
  1464. else
  1465. platform_pci_sleep_wake(dev, false);
  1466. pci_pme_active(dev, false);
  1467. dev->wakeup_prepared = false;
  1468. }
  1469. return ret;
  1470. }
  1471. EXPORT_SYMBOL(__pci_enable_wake);
  1472. /**
  1473. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1474. * @dev: PCI device to prepare
  1475. * @enable: True to enable wake-up event generation; false to disable
  1476. *
  1477. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1478. * and this function allows them to set that up cleanly - pci_enable_wake()
  1479. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1480. * ordering constraints.
  1481. *
  1482. * This function only returns error code if the device is not capable of
  1483. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  1484. * enable wake-up power for it.
  1485. */
  1486. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1487. {
  1488. return pci_pme_capable(dev, PCI_D3cold) ?
  1489. pci_enable_wake(dev, PCI_D3cold, enable) :
  1490. pci_enable_wake(dev, PCI_D3hot, enable);
  1491. }
  1492. /**
  1493. * pci_target_state - find an appropriate low power state for a given PCI dev
  1494. * @dev: PCI device
  1495. *
  1496. * Use underlying platform code to find a supported low power state for @dev.
  1497. * If the platform can't manage @dev, return the deepest state from which it
  1498. * can generate wake events, based on any available PME info.
  1499. */
  1500. pci_power_t pci_target_state(struct pci_dev *dev)
  1501. {
  1502. pci_power_t target_state = PCI_D3hot;
  1503. if (platform_pci_power_manageable(dev)) {
  1504. /*
  1505. * Call the platform to choose the target state of the device
  1506. * and enable wake-up from this state if supported.
  1507. */
  1508. pci_power_t state = platform_pci_choose_state(dev);
  1509. switch (state) {
  1510. case PCI_POWER_ERROR:
  1511. case PCI_UNKNOWN:
  1512. break;
  1513. case PCI_D1:
  1514. case PCI_D2:
  1515. if (pci_no_d1d2(dev))
  1516. break;
  1517. default:
  1518. target_state = state;
  1519. }
  1520. } else if (!dev->pm_cap) {
  1521. target_state = PCI_D0;
  1522. } else if (device_may_wakeup(&dev->dev)) {
  1523. /*
  1524. * Find the deepest state from which the device can generate
  1525. * wake-up events, make it the target state and enable device
  1526. * to generate PME#.
  1527. */
  1528. if (dev->pme_support) {
  1529. while (target_state
  1530. && !(dev->pme_support & (1 << target_state)))
  1531. target_state--;
  1532. }
  1533. }
  1534. return target_state;
  1535. }
  1536. /**
  1537. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1538. * @dev: Device to handle.
  1539. *
  1540. * Choose the power state appropriate for the device depending on whether
  1541. * it can wake up the system and/or is power manageable by the platform
  1542. * (PCI_D3hot is the default) and put the device into that state.
  1543. */
  1544. int pci_prepare_to_sleep(struct pci_dev *dev)
  1545. {
  1546. pci_power_t target_state = pci_target_state(dev);
  1547. int error;
  1548. if (target_state == PCI_POWER_ERROR)
  1549. return -EIO;
  1550. /* D3cold during system suspend/hibernate is not supported */
  1551. if (target_state > PCI_D3hot)
  1552. target_state = PCI_D3hot;
  1553. pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev));
  1554. error = pci_set_power_state(dev, target_state);
  1555. if (error)
  1556. pci_enable_wake(dev, target_state, false);
  1557. return error;
  1558. }
  1559. /**
  1560. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1561. * @dev: Device to handle.
  1562. *
  1563. * Disable device's system wake-up capability and put it into D0.
  1564. */
  1565. int pci_back_from_sleep(struct pci_dev *dev)
  1566. {
  1567. pci_enable_wake(dev, PCI_D0, false);
  1568. return pci_set_power_state(dev, PCI_D0);
  1569. }
  1570. /**
  1571. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  1572. * @dev: PCI device being suspended.
  1573. *
  1574. * Prepare @dev to generate wake-up events at run time and put it into a low
  1575. * power state.
  1576. */
  1577. int pci_finish_runtime_suspend(struct pci_dev *dev)
  1578. {
  1579. pci_power_t target_state = pci_target_state(dev);
  1580. int error;
  1581. if (target_state == PCI_POWER_ERROR)
  1582. return -EIO;
  1583. dev->runtime_d3cold = target_state == PCI_D3cold;
  1584. __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev));
  1585. error = pci_set_power_state(dev, target_state);
  1586. if (error) {
  1587. __pci_enable_wake(dev, target_state, true, false);
  1588. dev->runtime_d3cold = false;
  1589. }
  1590. return error;
  1591. }
  1592. /**
  1593. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  1594. * @dev: Device to check.
  1595. *
  1596. * Return true if the device itself is cabable of generating wake-up events
  1597. * (through the platform or using the native PCIe PME) or if the device supports
  1598. * PME and one of its upstream bridges can generate wake-up events.
  1599. */
  1600. bool pci_dev_run_wake(struct pci_dev *dev)
  1601. {
  1602. struct pci_bus *bus = dev->bus;
  1603. if (device_run_wake(&dev->dev))
  1604. return true;
  1605. if (!dev->pme_support)
  1606. return false;
  1607. while (bus->parent) {
  1608. struct pci_dev *bridge = bus->self;
  1609. if (device_run_wake(&bridge->dev))
  1610. return true;
  1611. bus = bus->parent;
  1612. }
  1613. /* We have reached the root bus. */
  1614. if (bus->bridge)
  1615. return device_run_wake(bus->bridge);
  1616. return false;
  1617. }
  1618. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  1619. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  1620. {
  1621. struct device *dev = &pdev->dev;
  1622. struct device *parent = dev->parent;
  1623. if (parent)
  1624. pm_runtime_get_sync(parent);
  1625. pm_runtime_get_noresume(dev);
  1626. /*
  1627. * pdev->current_state is set to PCI_D3cold during suspending,
  1628. * so wait until suspending completes
  1629. */
  1630. pm_runtime_barrier(dev);
  1631. /*
  1632. * Only need to resume devices in D3cold, because config
  1633. * registers are still accessible for devices suspended but
  1634. * not in D3cold.
  1635. */
  1636. if (pdev->current_state == PCI_D3cold)
  1637. pm_runtime_resume(dev);
  1638. }
  1639. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  1640. {
  1641. struct device *dev = &pdev->dev;
  1642. struct device *parent = dev->parent;
  1643. pm_runtime_put(dev);
  1644. if (parent)
  1645. pm_runtime_put_sync(parent);
  1646. }
  1647. /**
  1648. * pci_pm_init - Initialize PM functions of given PCI device
  1649. * @dev: PCI device to handle.
  1650. */
  1651. void pci_pm_init(struct pci_dev *dev)
  1652. {
  1653. int pm;
  1654. u16 pmc;
  1655. pm_runtime_forbid(&dev->dev);
  1656. pm_runtime_set_active(&dev->dev);
  1657. pm_runtime_enable(&dev->dev);
  1658. device_enable_async_suspend(&dev->dev);
  1659. dev->wakeup_prepared = false;
  1660. dev->pm_cap = 0;
  1661. dev->pme_support = 0;
  1662. /* find PCI PM capability in list */
  1663. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1664. if (!pm)
  1665. return;
  1666. /* Check device's ability to generate PME# */
  1667. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1668. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1669. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1670. pmc & PCI_PM_CAP_VER_MASK);
  1671. return;
  1672. }
  1673. dev->pm_cap = pm;
  1674. dev->d3_delay = PCI_PM_D3_WAIT;
  1675. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  1676. dev->d3cold_allowed = true;
  1677. dev->d1_support = false;
  1678. dev->d2_support = false;
  1679. if (!pci_no_d1d2(dev)) {
  1680. if (pmc & PCI_PM_CAP_D1)
  1681. dev->d1_support = true;
  1682. if (pmc & PCI_PM_CAP_D2)
  1683. dev->d2_support = true;
  1684. if (dev->d1_support || dev->d2_support)
  1685. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1686. dev->d1_support ? " D1" : "",
  1687. dev->d2_support ? " D2" : "");
  1688. }
  1689. pmc &= PCI_PM_CAP_PME_MASK;
  1690. if (pmc) {
  1691. dev_printk(KERN_DEBUG, &dev->dev,
  1692. "PME# supported from%s%s%s%s%s\n",
  1693. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1694. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1695. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1696. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1697. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1698. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1699. dev->pme_poll = true;
  1700. /*
  1701. * Make device's PM flags reflect the wake-up capability, but
  1702. * let the user space enable it to wake up the system as needed.
  1703. */
  1704. device_set_wakeup_capable(&dev->dev, true);
  1705. /* Disable the PME# generation functionality */
  1706. pci_pme_active(dev, false);
  1707. }
  1708. }
  1709. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  1710. struct pci_cap_saved_state *new_cap)
  1711. {
  1712. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  1713. }
  1714. /**
  1715. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1716. * @dev: the PCI device
  1717. * @cap: the capability to allocate the buffer for
  1718. * @size: requested size of the buffer
  1719. */
  1720. static int pci_add_cap_save_buffer(
  1721. struct pci_dev *dev, char cap, unsigned int size)
  1722. {
  1723. int pos;
  1724. struct pci_cap_saved_state *save_state;
  1725. pos = pci_find_capability(dev, cap);
  1726. if (pos <= 0)
  1727. return 0;
  1728. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1729. if (!save_state)
  1730. return -ENOMEM;
  1731. save_state->cap.cap_nr = cap;
  1732. save_state->cap.size = size;
  1733. pci_add_saved_cap(dev, save_state);
  1734. return 0;
  1735. }
  1736. /**
  1737. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1738. * @dev: the PCI device
  1739. */
  1740. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1741. {
  1742. int error;
  1743. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  1744. PCI_EXP_SAVE_REGS * sizeof(u16));
  1745. if (error)
  1746. dev_err(&dev->dev,
  1747. "unable to preallocate PCI Express save buffer\n");
  1748. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1749. if (error)
  1750. dev_err(&dev->dev,
  1751. "unable to preallocate PCI-X save buffer\n");
  1752. }
  1753. void pci_free_cap_save_buffers(struct pci_dev *dev)
  1754. {
  1755. struct pci_cap_saved_state *tmp;
  1756. struct hlist_node *n;
  1757. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  1758. kfree(tmp);
  1759. }
  1760. /**
  1761. * pci_configure_ari - enable or disable ARI forwarding
  1762. * @dev: the PCI device
  1763. *
  1764. * If @dev and its upstream bridge both support ARI, enable ARI in the
  1765. * bridge. Otherwise, disable ARI in the bridge.
  1766. */
  1767. void pci_configure_ari(struct pci_dev *dev)
  1768. {
  1769. u32 cap;
  1770. struct pci_dev *bridge;
  1771. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  1772. return;
  1773. bridge = dev->bus->self;
  1774. if (!bridge)
  1775. return;
  1776. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  1777. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1778. return;
  1779. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  1780. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  1781. PCI_EXP_DEVCTL2_ARI);
  1782. bridge->ari_enabled = 1;
  1783. } else {
  1784. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  1785. PCI_EXP_DEVCTL2_ARI);
  1786. bridge->ari_enabled = 0;
  1787. }
  1788. }
  1789. /**
  1790. * pci_enable_ido - enable ID-based Ordering on a device
  1791. * @dev: the PCI device
  1792. * @type: which types of IDO to enable
  1793. *
  1794. * Enable ID-based ordering on @dev. @type can contain the bits
  1795. * %PCI_EXP_IDO_REQUEST and/or %PCI_EXP_IDO_COMPLETION to indicate
  1796. * which types of transactions are allowed to be re-ordered.
  1797. */
  1798. void pci_enable_ido(struct pci_dev *dev, unsigned long type)
  1799. {
  1800. u16 ctrl = 0;
  1801. if (type & PCI_EXP_IDO_REQUEST)
  1802. ctrl |= PCI_EXP_IDO_REQ_EN;
  1803. if (type & PCI_EXP_IDO_COMPLETION)
  1804. ctrl |= PCI_EXP_IDO_CMP_EN;
  1805. if (ctrl)
  1806. pcie_capability_set_word(dev, PCI_EXP_DEVCTL2, ctrl);
  1807. }
  1808. EXPORT_SYMBOL(pci_enable_ido);
  1809. /**
  1810. * pci_disable_ido - disable ID-based ordering on a device
  1811. * @dev: the PCI device
  1812. * @type: which types of IDO to disable
  1813. */
  1814. void pci_disable_ido(struct pci_dev *dev, unsigned long type)
  1815. {
  1816. u16 ctrl = 0;
  1817. if (type & PCI_EXP_IDO_REQUEST)
  1818. ctrl |= PCI_EXP_IDO_REQ_EN;
  1819. if (type & PCI_EXP_IDO_COMPLETION)
  1820. ctrl |= PCI_EXP_IDO_CMP_EN;
  1821. if (ctrl)
  1822. pcie_capability_clear_word(dev, PCI_EXP_DEVCTL2, ctrl);
  1823. }
  1824. EXPORT_SYMBOL(pci_disable_ido);
  1825. /**
  1826. * pci_enable_obff - enable optimized buffer flush/fill
  1827. * @dev: PCI device
  1828. * @type: type of signaling to use
  1829. *
  1830. * Try to enable @type OBFF signaling on @dev. It will try using WAKE#
  1831. * signaling if possible, falling back to message signaling only if
  1832. * WAKE# isn't supported. @type should indicate whether the PCIe link
  1833. * be brought out of L0s or L1 to send the message. It should be either
  1834. * %PCI_EXP_OBFF_SIGNAL_ALWAYS or %PCI_OBFF_SIGNAL_L0.
  1835. *
  1836. * If your device can benefit from receiving all messages, even at the
  1837. * power cost of bringing the link back up from a low power state, use
  1838. * %PCI_EXP_OBFF_SIGNAL_ALWAYS. Otherwise, use %PCI_OBFF_SIGNAL_L0 (the
  1839. * preferred type).
  1840. *
  1841. * RETURNS:
  1842. * Zero on success, appropriate error number on failure.
  1843. */
  1844. int pci_enable_obff(struct pci_dev *dev, enum pci_obff_signal_type type)
  1845. {
  1846. u32 cap;
  1847. u16 ctrl;
  1848. int ret;
  1849. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
  1850. if (!(cap & PCI_EXP_OBFF_MASK))
  1851. return -ENOTSUPP; /* no OBFF support at all */
  1852. /* Make sure the topology supports OBFF as well */
  1853. if (dev->bus->self) {
  1854. ret = pci_enable_obff(dev->bus->self, type);
  1855. if (ret)
  1856. return ret;
  1857. }
  1858. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &ctrl);
  1859. if (cap & PCI_EXP_OBFF_WAKE)
  1860. ctrl |= PCI_EXP_OBFF_WAKE_EN;
  1861. else {
  1862. switch (type) {
  1863. case PCI_EXP_OBFF_SIGNAL_L0:
  1864. if (!(ctrl & PCI_EXP_OBFF_WAKE_EN))
  1865. ctrl |= PCI_EXP_OBFF_MSGA_EN;
  1866. break;
  1867. case PCI_EXP_OBFF_SIGNAL_ALWAYS:
  1868. ctrl &= ~PCI_EXP_OBFF_WAKE_EN;
  1869. ctrl |= PCI_EXP_OBFF_MSGB_EN;
  1870. break;
  1871. default:
  1872. WARN(1, "bad OBFF signal type\n");
  1873. return -ENOTSUPP;
  1874. }
  1875. }
  1876. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, ctrl);
  1877. return 0;
  1878. }
  1879. EXPORT_SYMBOL(pci_enable_obff);
  1880. /**
  1881. * pci_disable_obff - disable optimized buffer flush/fill
  1882. * @dev: PCI device
  1883. *
  1884. * Disable OBFF on @dev.
  1885. */
  1886. void pci_disable_obff(struct pci_dev *dev)
  1887. {
  1888. pcie_capability_clear_word(dev, PCI_EXP_DEVCTL2, PCI_EXP_OBFF_WAKE_EN);
  1889. }
  1890. EXPORT_SYMBOL(pci_disable_obff);
  1891. /**
  1892. * pci_ltr_supported - check whether a device supports LTR
  1893. * @dev: PCI device
  1894. *
  1895. * RETURNS:
  1896. * True if @dev supports latency tolerance reporting, false otherwise.
  1897. */
  1898. static bool pci_ltr_supported(struct pci_dev *dev)
  1899. {
  1900. u32 cap;
  1901. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
  1902. return cap & PCI_EXP_DEVCAP2_LTR;
  1903. }
  1904. /**
  1905. * pci_enable_ltr - enable latency tolerance reporting
  1906. * @dev: PCI device
  1907. *
  1908. * Enable LTR on @dev if possible, which means enabling it first on
  1909. * upstream ports.
  1910. *
  1911. * RETURNS:
  1912. * Zero on success, errno on failure.
  1913. */
  1914. int pci_enable_ltr(struct pci_dev *dev)
  1915. {
  1916. int ret;
  1917. /* Only primary function can enable/disable LTR */
  1918. if (PCI_FUNC(dev->devfn) != 0)
  1919. return -EINVAL;
  1920. if (!pci_ltr_supported(dev))
  1921. return -ENOTSUPP;
  1922. /* Enable upstream ports first */
  1923. if (dev->bus->self) {
  1924. ret = pci_enable_ltr(dev->bus->self);
  1925. if (ret)
  1926. return ret;
  1927. }
  1928. return pcie_capability_set_word(dev, PCI_EXP_DEVCTL2, PCI_EXP_LTR_EN);
  1929. }
  1930. EXPORT_SYMBOL(pci_enable_ltr);
  1931. /**
  1932. * pci_disable_ltr - disable latency tolerance reporting
  1933. * @dev: PCI device
  1934. */
  1935. void pci_disable_ltr(struct pci_dev *dev)
  1936. {
  1937. /* Only primary function can enable/disable LTR */
  1938. if (PCI_FUNC(dev->devfn) != 0)
  1939. return;
  1940. if (!pci_ltr_supported(dev))
  1941. return;
  1942. pcie_capability_clear_word(dev, PCI_EXP_DEVCTL2, PCI_EXP_LTR_EN);
  1943. }
  1944. EXPORT_SYMBOL(pci_disable_ltr);
  1945. static int __pci_ltr_scale(int *val)
  1946. {
  1947. int scale = 0;
  1948. while (*val > 1023) {
  1949. *val = (*val + 31) / 32;
  1950. scale++;
  1951. }
  1952. return scale;
  1953. }
  1954. /**
  1955. * pci_set_ltr - set LTR latency values
  1956. * @dev: PCI device
  1957. * @snoop_lat_ns: snoop latency in nanoseconds
  1958. * @nosnoop_lat_ns: nosnoop latency in nanoseconds
  1959. *
  1960. * Figure out the scale and set the LTR values accordingly.
  1961. */
  1962. int pci_set_ltr(struct pci_dev *dev, int snoop_lat_ns, int nosnoop_lat_ns)
  1963. {
  1964. int pos, ret, snoop_scale, nosnoop_scale;
  1965. u16 val;
  1966. if (!pci_ltr_supported(dev))
  1967. return -ENOTSUPP;
  1968. snoop_scale = __pci_ltr_scale(&snoop_lat_ns);
  1969. nosnoop_scale = __pci_ltr_scale(&nosnoop_lat_ns);
  1970. if (snoop_lat_ns > PCI_LTR_VALUE_MASK ||
  1971. nosnoop_lat_ns > PCI_LTR_VALUE_MASK)
  1972. return -EINVAL;
  1973. if ((snoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)) ||
  1974. (nosnoop_scale > (PCI_LTR_SCALE_MASK >> PCI_LTR_SCALE_SHIFT)))
  1975. return -EINVAL;
  1976. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
  1977. if (!pos)
  1978. return -ENOTSUPP;
  1979. val = (snoop_scale << PCI_LTR_SCALE_SHIFT) | snoop_lat_ns;
  1980. ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_SNOOP_LAT, val);
  1981. if (ret != 4)
  1982. return -EIO;
  1983. val = (nosnoop_scale << PCI_LTR_SCALE_SHIFT) | nosnoop_lat_ns;
  1984. ret = pci_write_config_word(dev, pos + PCI_LTR_MAX_NOSNOOP_LAT, val);
  1985. if (ret != 4)
  1986. return -EIO;
  1987. return 0;
  1988. }
  1989. EXPORT_SYMBOL(pci_set_ltr);
  1990. static int pci_acs_enable;
  1991. /**
  1992. * pci_request_acs - ask for ACS to be enabled if supported
  1993. */
  1994. void pci_request_acs(void)
  1995. {
  1996. pci_acs_enable = 1;
  1997. }
  1998. /**
  1999. * pci_enable_acs - enable ACS if hardware support it
  2000. * @dev: the PCI device
  2001. */
  2002. void pci_enable_acs(struct pci_dev *dev)
  2003. {
  2004. int pos;
  2005. u16 cap;
  2006. u16 ctrl;
  2007. if (!pci_acs_enable)
  2008. return;
  2009. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2010. if (!pos)
  2011. return;
  2012. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  2013. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2014. /* Source Validation */
  2015. ctrl |= (cap & PCI_ACS_SV);
  2016. /* P2P Request Redirect */
  2017. ctrl |= (cap & PCI_ACS_RR);
  2018. /* P2P Completion Redirect */
  2019. ctrl |= (cap & PCI_ACS_CR);
  2020. /* Upstream Forwarding */
  2021. ctrl |= (cap & PCI_ACS_UF);
  2022. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2023. }
  2024. /**
  2025. * pci_acs_enabled - test ACS against required flags for a given device
  2026. * @pdev: device to test
  2027. * @acs_flags: required PCI ACS flags
  2028. *
  2029. * Return true if the device supports the provided flags. Automatically
  2030. * filters out flags that are not implemented on multifunction devices.
  2031. */
  2032. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  2033. {
  2034. int pos, ret;
  2035. u16 ctrl;
  2036. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  2037. if (ret >= 0)
  2038. return ret > 0;
  2039. if (!pci_is_pcie(pdev))
  2040. return false;
  2041. /* Filter out flags not applicable to multifunction */
  2042. if (pdev->multifunction)
  2043. acs_flags &= (PCI_ACS_RR | PCI_ACS_CR |
  2044. PCI_ACS_EC | PCI_ACS_DT);
  2045. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM ||
  2046. pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
  2047. pdev->multifunction) {
  2048. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  2049. if (!pos)
  2050. return false;
  2051. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  2052. if ((ctrl & acs_flags) != acs_flags)
  2053. return false;
  2054. }
  2055. return true;
  2056. }
  2057. /**
  2058. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  2059. * @start: starting downstream device
  2060. * @end: ending upstream device or NULL to search to the root bus
  2061. * @acs_flags: required flags
  2062. *
  2063. * Walk up a device tree from start to end testing PCI ACS support. If
  2064. * any step along the way does not support the required flags, return false.
  2065. */
  2066. bool pci_acs_path_enabled(struct pci_dev *start,
  2067. struct pci_dev *end, u16 acs_flags)
  2068. {
  2069. struct pci_dev *pdev, *parent = start;
  2070. do {
  2071. pdev = parent;
  2072. if (!pci_acs_enabled(pdev, acs_flags))
  2073. return false;
  2074. if (pci_is_root_bus(pdev->bus))
  2075. return (end == NULL);
  2076. parent = pdev->bus->self;
  2077. } while (pdev != end);
  2078. return true;
  2079. }
  2080. /**
  2081. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2082. * @dev: the PCI device
  2083. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2084. *
  2085. * Perform INTx swizzling for a device behind one level of bridge. This is
  2086. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2087. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2088. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2089. * the PCI Express Base Specification, Revision 2.1)
  2090. */
  2091. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2092. {
  2093. int slot;
  2094. if (pci_ari_enabled(dev->bus))
  2095. slot = 0;
  2096. else
  2097. slot = PCI_SLOT(dev->devfn);
  2098. return (((pin - 1) + slot) % 4) + 1;
  2099. }
  2100. int
  2101. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2102. {
  2103. u8 pin;
  2104. pin = dev->pin;
  2105. if (!pin)
  2106. return -1;
  2107. while (!pci_is_root_bus(dev->bus)) {
  2108. pin = pci_swizzle_interrupt_pin(dev, pin);
  2109. dev = dev->bus->self;
  2110. }
  2111. *bridge = dev;
  2112. return pin;
  2113. }
  2114. /**
  2115. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2116. * @dev: the PCI device
  2117. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2118. *
  2119. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  2120. * bridges all the way up to a PCI root bus.
  2121. */
  2122. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  2123. {
  2124. u8 pin = *pinp;
  2125. while (!pci_is_root_bus(dev->bus)) {
  2126. pin = pci_swizzle_interrupt_pin(dev, pin);
  2127. dev = dev->bus->self;
  2128. }
  2129. *pinp = pin;
  2130. return PCI_SLOT(dev->devfn);
  2131. }
  2132. /**
  2133. * pci_release_region - Release a PCI bar
  2134. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  2135. * @bar: BAR to release
  2136. *
  2137. * Releases the PCI I/O and memory resources previously reserved by a
  2138. * successful call to pci_request_region. Call this function only
  2139. * after all use of the PCI regions has ceased.
  2140. */
  2141. void pci_release_region(struct pci_dev *pdev, int bar)
  2142. {
  2143. struct pci_devres *dr;
  2144. if (pci_resource_len(pdev, bar) == 0)
  2145. return;
  2146. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  2147. release_region(pci_resource_start(pdev, bar),
  2148. pci_resource_len(pdev, bar));
  2149. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  2150. release_mem_region(pci_resource_start(pdev, bar),
  2151. pci_resource_len(pdev, bar));
  2152. dr = find_pci_dr(pdev);
  2153. if (dr)
  2154. dr->region_mask &= ~(1 << bar);
  2155. }
  2156. /**
  2157. * __pci_request_region - Reserved PCI I/O and memory resource
  2158. * @pdev: PCI device whose resources are to be reserved
  2159. * @bar: BAR to be reserved
  2160. * @res_name: Name to be associated with resource.
  2161. * @exclusive: whether the region access is exclusive or not
  2162. *
  2163. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2164. * being reserved by owner @res_name. Do not access any
  2165. * address inside the PCI regions unless this call returns
  2166. * successfully.
  2167. *
  2168. * If @exclusive is set, then the region is marked so that userspace
  2169. * is explicitly not allowed to map the resource via /dev/mem or
  2170. * sysfs MMIO access.
  2171. *
  2172. * Returns 0 on success, or %EBUSY on error. A warning
  2173. * message is also printed on failure.
  2174. */
  2175. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  2176. int exclusive)
  2177. {
  2178. struct pci_devres *dr;
  2179. if (pci_resource_len(pdev, bar) == 0)
  2180. return 0;
  2181. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  2182. if (!request_region(pci_resource_start(pdev, bar),
  2183. pci_resource_len(pdev, bar), res_name))
  2184. goto err_out;
  2185. }
  2186. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  2187. if (!__request_mem_region(pci_resource_start(pdev, bar),
  2188. pci_resource_len(pdev, bar), res_name,
  2189. exclusive))
  2190. goto err_out;
  2191. }
  2192. dr = find_pci_dr(pdev);
  2193. if (dr)
  2194. dr->region_mask |= 1 << bar;
  2195. return 0;
  2196. err_out:
  2197. dev_warn(&pdev->dev, "BAR %d: can't reserve %pR\n", bar,
  2198. &pdev->resource[bar]);
  2199. return -EBUSY;
  2200. }
  2201. /**
  2202. * pci_request_region - Reserve PCI I/O and memory resource
  2203. * @pdev: PCI device whose resources are to be reserved
  2204. * @bar: BAR to be reserved
  2205. * @res_name: Name to be associated with resource
  2206. *
  2207. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  2208. * being reserved by owner @res_name. Do not access any
  2209. * address inside the PCI regions unless this call returns
  2210. * successfully.
  2211. *
  2212. * Returns 0 on success, or %EBUSY on error. A warning
  2213. * message is also printed on failure.
  2214. */
  2215. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  2216. {
  2217. return __pci_request_region(pdev, bar, res_name, 0);
  2218. }
  2219. /**
  2220. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  2221. * @pdev: PCI device whose resources are to be reserved
  2222. * @bar: BAR to be reserved
  2223. * @res_name: Name to be associated with resource.
  2224. *
  2225. * Mark the PCI region associated with PCI device @pdev BR @bar as
  2226. * being reserved by owner @res_name. Do not access any
  2227. * address inside the PCI regions unless this call returns
  2228. * successfully.
  2229. *
  2230. * Returns 0 on success, or %EBUSY on error. A warning
  2231. * message is also printed on failure.
  2232. *
  2233. * The key difference that _exclusive makes it that userspace is
  2234. * explicitly not allowed to map the resource via /dev/mem or
  2235. * sysfs.
  2236. */
  2237. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  2238. {
  2239. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  2240. }
  2241. /**
  2242. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  2243. * @pdev: PCI device whose resources were previously reserved
  2244. * @bars: Bitmask of BARs to be released
  2245. *
  2246. * Release selected PCI I/O and memory resources previously reserved.
  2247. * Call this function only after all use of the PCI regions has ceased.
  2248. */
  2249. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  2250. {
  2251. int i;
  2252. for (i = 0; i < 6; i++)
  2253. if (bars & (1 << i))
  2254. pci_release_region(pdev, i);
  2255. }
  2256. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2257. const char *res_name, int excl)
  2258. {
  2259. int i;
  2260. for (i = 0; i < 6; i++)
  2261. if (bars & (1 << i))
  2262. if (__pci_request_region(pdev, i, res_name, excl))
  2263. goto err_out;
  2264. return 0;
  2265. err_out:
  2266. while(--i >= 0)
  2267. if (bars & (1 << i))
  2268. pci_release_region(pdev, i);
  2269. return -EBUSY;
  2270. }
  2271. /**
  2272. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  2273. * @pdev: PCI device whose resources are to be reserved
  2274. * @bars: Bitmask of BARs to be requested
  2275. * @res_name: Name to be associated with resource
  2276. */
  2277. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  2278. const char *res_name)
  2279. {
  2280. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  2281. }
  2282. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  2283. int bars, const char *res_name)
  2284. {
  2285. return __pci_request_selected_regions(pdev, bars, res_name,
  2286. IORESOURCE_EXCLUSIVE);
  2287. }
  2288. /**
  2289. * pci_release_regions - Release reserved PCI I/O and memory resources
  2290. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  2291. *
  2292. * Releases all PCI I/O and memory resources previously reserved by a
  2293. * successful call to pci_request_regions. Call this function only
  2294. * after all use of the PCI regions has ceased.
  2295. */
  2296. void pci_release_regions(struct pci_dev *pdev)
  2297. {
  2298. pci_release_selected_regions(pdev, (1 << 6) - 1);
  2299. }
  2300. /**
  2301. * pci_request_regions - Reserved PCI I/O and memory resources
  2302. * @pdev: PCI device whose resources are to be reserved
  2303. * @res_name: Name to be associated with resource.
  2304. *
  2305. * Mark all PCI regions associated with PCI device @pdev as
  2306. * being reserved by owner @res_name. Do not access any
  2307. * address inside the PCI regions unless this call returns
  2308. * successfully.
  2309. *
  2310. * Returns 0 on success, or %EBUSY on error. A warning
  2311. * message is also printed on failure.
  2312. */
  2313. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  2314. {
  2315. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  2316. }
  2317. /**
  2318. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  2319. * @pdev: PCI device whose resources are to be reserved
  2320. * @res_name: Name to be associated with resource.
  2321. *
  2322. * Mark all PCI regions associated with PCI device @pdev as
  2323. * being reserved by owner @res_name. Do not access any
  2324. * address inside the PCI regions unless this call returns
  2325. * successfully.
  2326. *
  2327. * pci_request_regions_exclusive() will mark the region so that
  2328. * /dev/mem and the sysfs MMIO access will not be allowed.
  2329. *
  2330. * Returns 0 on success, or %EBUSY on error. A warning
  2331. * message is also printed on failure.
  2332. */
  2333. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  2334. {
  2335. return pci_request_selected_regions_exclusive(pdev,
  2336. ((1 << 6) - 1), res_name);
  2337. }
  2338. static void __pci_set_master(struct pci_dev *dev, bool enable)
  2339. {
  2340. u16 old_cmd, cmd;
  2341. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  2342. if (enable)
  2343. cmd = old_cmd | PCI_COMMAND_MASTER;
  2344. else
  2345. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  2346. if (cmd != old_cmd) {
  2347. dev_dbg(&dev->dev, "%s bus mastering\n",
  2348. enable ? "enabling" : "disabling");
  2349. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2350. }
  2351. dev->is_busmaster = enable;
  2352. }
  2353. /**
  2354. * pcibios_setup - process "pci=" kernel boot arguments
  2355. * @str: string used to pass in "pci=" kernel boot arguments
  2356. *
  2357. * Process kernel boot arguments. This is the default implementation.
  2358. * Architecture specific implementations can override this as necessary.
  2359. */
  2360. char * __weak __init pcibios_setup(char *str)
  2361. {
  2362. return str;
  2363. }
  2364. /**
  2365. * pcibios_set_master - enable PCI bus-mastering for device dev
  2366. * @dev: the PCI device to enable
  2367. *
  2368. * Enables PCI bus-mastering for the device. This is the default
  2369. * implementation. Architecture specific implementations can override
  2370. * this if necessary.
  2371. */
  2372. void __weak pcibios_set_master(struct pci_dev *dev)
  2373. {
  2374. u8 lat;
  2375. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  2376. if (pci_is_pcie(dev))
  2377. return;
  2378. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  2379. if (lat < 16)
  2380. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  2381. else if (lat > pcibios_max_latency)
  2382. lat = pcibios_max_latency;
  2383. else
  2384. return;
  2385. dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
  2386. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  2387. }
  2388. /**
  2389. * pci_set_master - enables bus-mastering for device dev
  2390. * @dev: the PCI device to enable
  2391. *
  2392. * Enables bus-mastering on the device and calls pcibios_set_master()
  2393. * to do the needed arch specific settings.
  2394. */
  2395. void pci_set_master(struct pci_dev *dev)
  2396. {
  2397. __pci_set_master(dev, true);
  2398. pcibios_set_master(dev);
  2399. }
  2400. /**
  2401. * pci_clear_master - disables bus-mastering for device dev
  2402. * @dev: the PCI device to disable
  2403. */
  2404. void pci_clear_master(struct pci_dev *dev)
  2405. {
  2406. __pci_set_master(dev, false);
  2407. }
  2408. /**
  2409. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  2410. * @dev: the PCI device for which MWI is to be enabled
  2411. *
  2412. * Helper function for pci_set_mwi.
  2413. * Originally copied from drivers/net/acenic.c.
  2414. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  2415. *
  2416. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2417. */
  2418. int pci_set_cacheline_size(struct pci_dev *dev)
  2419. {
  2420. u8 cacheline_size;
  2421. if (!pci_cache_line_size)
  2422. return -EINVAL;
  2423. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  2424. equal to or multiple of the right value. */
  2425. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2426. if (cacheline_size >= pci_cache_line_size &&
  2427. (cacheline_size % pci_cache_line_size) == 0)
  2428. return 0;
  2429. /* Write the correct value. */
  2430. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  2431. /* Read it back. */
  2432. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  2433. if (cacheline_size == pci_cache_line_size)
  2434. return 0;
  2435. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  2436. "supported\n", pci_cache_line_size << 2);
  2437. return -EINVAL;
  2438. }
  2439. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  2440. #ifdef PCI_DISABLE_MWI
  2441. int pci_set_mwi(struct pci_dev *dev)
  2442. {
  2443. return 0;
  2444. }
  2445. int pci_try_set_mwi(struct pci_dev *dev)
  2446. {
  2447. return 0;
  2448. }
  2449. void pci_clear_mwi(struct pci_dev *dev)
  2450. {
  2451. }
  2452. #else
  2453. /**
  2454. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  2455. * @dev: the PCI device for which MWI is enabled
  2456. *
  2457. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2458. *
  2459. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2460. */
  2461. int
  2462. pci_set_mwi(struct pci_dev *dev)
  2463. {
  2464. int rc;
  2465. u16 cmd;
  2466. rc = pci_set_cacheline_size(dev);
  2467. if (rc)
  2468. return rc;
  2469. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2470. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  2471. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  2472. cmd |= PCI_COMMAND_INVALIDATE;
  2473. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2474. }
  2475. return 0;
  2476. }
  2477. /**
  2478. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  2479. * @dev: the PCI device for which MWI is enabled
  2480. *
  2481. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  2482. * Callers are not required to check the return value.
  2483. *
  2484. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  2485. */
  2486. int pci_try_set_mwi(struct pci_dev *dev)
  2487. {
  2488. int rc = pci_set_mwi(dev);
  2489. return rc;
  2490. }
  2491. /**
  2492. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  2493. * @dev: the PCI device to disable
  2494. *
  2495. * Disables PCI Memory-Write-Invalidate transaction on the device
  2496. */
  2497. void
  2498. pci_clear_mwi(struct pci_dev *dev)
  2499. {
  2500. u16 cmd;
  2501. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  2502. if (cmd & PCI_COMMAND_INVALIDATE) {
  2503. cmd &= ~PCI_COMMAND_INVALIDATE;
  2504. pci_write_config_word(dev, PCI_COMMAND, cmd);
  2505. }
  2506. }
  2507. #endif /* ! PCI_DISABLE_MWI */
  2508. /**
  2509. * pci_intx - enables/disables PCI INTx for device dev
  2510. * @pdev: the PCI device to operate on
  2511. * @enable: boolean: whether to enable or disable PCI INTx
  2512. *
  2513. * Enables/disables PCI INTx for device dev
  2514. */
  2515. void
  2516. pci_intx(struct pci_dev *pdev, int enable)
  2517. {
  2518. u16 pci_command, new;
  2519. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  2520. if (enable) {
  2521. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  2522. } else {
  2523. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  2524. }
  2525. if (new != pci_command) {
  2526. struct pci_devres *dr;
  2527. pci_write_config_word(pdev, PCI_COMMAND, new);
  2528. dr = find_pci_dr(pdev);
  2529. if (dr && !dr->restore_intx) {
  2530. dr->restore_intx = 1;
  2531. dr->orig_intx = !enable;
  2532. }
  2533. }
  2534. }
  2535. /**
  2536. * pci_intx_mask_supported - probe for INTx masking support
  2537. * @dev: the PCI device to operate on
  2538. *
  2539. * Check if the device dev support INTx masking via the config space
  2540. * command word.
  2541. */
  2542. bool pci_intx_mask_supported(struct pci_dev *dev)
  2543. {
  2544. bool mask_supported = false;
  2545. u16 orig, new;
  2546. if (dev->broken_intx_masking)
  2547. return false;
  2548. pci_cfg_access_lock(dev);
  2549. pci_read_config_word(dev, PCI_COMMAND, &orig);
  2550. pci_write_config_word(dev, PCI_COMMAND,
  2551. orig ^ PCI_COMMAND_INTX_DISABLE);
  2552. pci_read_config_word(dev, PCI_COMMAND, &new);
  2553. /*
  2554. * There's no way to protect against hardware bugs or detect them
  2555. * reliably, but as long as we know what the value should be, let's
  2556. * go ahead and check it.
  2557. */
  2558. if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
  2559. dev_err(&dev->dev, "Command register changed from "
  2560. "0x%x to 0x%x: driver or hardware bug?\n", orig, new);
  2561. } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
  2562. mask_supported = true;
  2563. pci_write_config_word(dev, PCI_COMMAND, orig);
  2564. }
  2565. pci_cfg_access_unlock(dev);
  2566. return mask_supported;
  2567. }
  2568. EXPORT_SYMBOL_GPL(pci_intx_mask_supported);
  2569. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  2570. {
  2571. struct pci_bus *bus = dev->bus;
  2572. bool mask_updated = true;
  2573. u32 cmd_status_dword;
  2574. u16 origcmd, newcmd;
  2575. unsigned long flags;
  2576. bool irq_pending;
  2577. /*
  2578. * We do a single dword read to retrieve both command and status.
  2579. * Document assumptions that make this possible.
  2580. */
  2581. BUILD_BUG_ON(PCI_COMMAND % 4);
  2582. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  2583. raw_spin_lock_irqsave(&pci_lock, flags);
  2584. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  2585. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  2586. /*
  2587. * Check interrupt status register to see whether our device
  2588. * triggered the interrupt (when masking) or the next IRQ is
  2589. * already pending (when unmasking).
  2590. */
  2591. if (mask != irq_pending) {
  2592. mask_updated = false;
  2593. goto done;
  2594. }
  2595. origcmd = cmd_status_dword;
  2596. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  2597. if (mask)
  2598. newcmd |= PCI_COMMAND_INTX_DISABLE;
  2599. if (newcmd != origcmd)
  2600. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  2601. done:
  2602. raw_spin_unlock_irqrestore(&pci_lock, flags);
  2603. return mask_updated;
  2604. }
  2605. /**
  2606. * pci_check_and_mask_intx - mask INTx on pending interrupt
  2607. * @dev: the PCI device to operate on
  2608. *
  2609. * Check if the device dev has its INTx line asserted, mask it and
  2610. * return true in that case. False is returned if not interrupt was
  2611. * pending.
  2612. */
  2613. bool pci_check_and_mask_intx(struct pci_dev *dev)
  2614. {
  2615. return pci_check_and_set_intx_mask(dev, true);
  2616. }
  2617. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  2618. /**
  2619. * pci_check_and_mask_intx - unmask INTx of no interrupt is pending
  2620. * @dev: the PCI device to operate on
  2621. *
  2622. * Check if the device dev has its INTx line asserted, unmask it if not
  2623. * and return true. False is returned and the mask remains active if
  2624. * there was still an interrupt pending.
  2625. */
  2626. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  2627. {
  2628. return pci_check_and_set_intx_mask(dev, false);
  2629. }
  2630. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  2631. /**
  2632. * pci_msi_off - disables any msi or msix capabilities
  2633. * @dev: the PCI device to operate on
  2634. *
  2635. * If you want to use msi see pci_enable_msi and friends.
  2636. * This is a lower level primitive that allows us to disable
  2637. * msi operation at the device level.
  2638. */
  2639. void pci_msi_off(struct pci_dev *dev)
  2640. {
  2641. int pos;
  2642. u16 control;
  2643. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  2644. if (pos) {
  2645. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  2646. control &= ~PCI_MSI_FLAGS_ENABLE;
  2647. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  2648. }
  2649. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  2650. if (pos) {
  2651. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  2652. control &= ~PCI_MSIX_FLAGS_ENABLE;
  2653. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  2654. }
  2655. }
  2656. EXPORT_SYMBOL_GPL(pci_msi_off);
  2657. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  2658. {
  2659. return dma_set_max_seg_size(&dev->dev, size);
  2660. }
  2661. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  2662. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  2663. {
  2664. return dma_set_seg_boundary(&dev->dev, mask);
  2665. }
  2666. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  2667. static int pcie_flr(struct pci_dev *dev, int probe)
  2668. {
  2669. int i;
  2670. u32 cap;
  2671. u16 status;
  2672. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  2673. if (!(cap & PCI_EXP_DEVCAP_FLR))
  2674. return -ENOTTY;
  2675. if (probe)
  2676. return 0;
  2677. /* Wait for Transaction Pending bit clean */
  2678. for (i = 0; i < 4; i++) {
  2679. if (i)
  2680. msleep((1 << (i - 1)) * 100);
  2681. pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
  2682. if (!(status & PCI_EXP_DEVSTA_TRPND))
  2683. goto clear;
  2684. }
  2685. dev_err(&dev->dev, "transaction is not cleared; "
  2686. "proceeding with reset anyway\n");
  2687. clear:
  2688. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  2689. msleep(100);
  2690. return 0;
  2691. }
  2692. static int pci_af_flr(struct pci_dev *dev, int probe)
  2693. {
  2694. int i;
  2695. int pos;
  2696. u8 cap;
  2697. u8 status;
  2698. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  2699. if (!pos)
  2700. return -ENOTTY;
  2701. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  2702. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  2703. return -ENOTTY;
  2704. if (probe)
  2705. return 0;
  2706. /* Wait for Transaction Pending bit clean */
  2707. for (i = 0; i < 4; i++) {
  2708. if (i)
  2709. msleep((1 << (i - 1)) * 100);
  2710. pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
  2711. if (!(status & PCI_AF_STATUS_TP))
  2712. goto clear;
  2713. }
  2714. dev_err(&dev->dev, "transaction is not cleared; "
  2715. "proceeding with reset anyway\n");
  2716. clear:
  2717. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  2718. msleep(100);
  2719. return 0;
  2720. }
  2721. /**
  2722. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  2723. * @dev: Device to reset.
  2724. * @probe: If set, only check if the device can be reset this way.
  2725. *
  2726. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  2727. * unset, it will be reinitialized internally when going from PCI_D3hot to
  2728. * PCI_D0. If that's the case and the device is not in a low-power state
  2729. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  2730. *
  2731. * NOTE: This causes the caller to sleep for twice the device power transition
  2732. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  2733. * by devault (i.e. unless the @dev's d3_delay field has a different value).
  2734. * Moreover, only devices in D0 can be reset by this function.
  2735. */
  2736. static int pci_pm_reset(struct pci_dev *dev, int probe)
  2737. {
  2738. u16 csr;
  2739. if (!dev->pm_cap)
  2740. return -ENOTTY;
  2741. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  2742. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  2743. return -ENOTTY;
  2744. if (probe)
  2745. return 0;
  2746. if (dev->current_state != PCI_D0)
  2747. return -EINVAL;
  2748. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2749. csr |= PCI_D3hot;
  2750. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2751. pci_dev_d3_sleep(dev);
  2752. csr &= ~PCI_PM_CTRL_STATE_MASK;
  2753. csr |= PCI_D0;
  2754. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  2755. pci_dev_d3_sleep(dev);
  2756. return 0;
  2757. }
  2758. /**
  2759. * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
  2760. * @dev: Bridge device
  2761. *
  2762. * Use the bridge control register to assert reset on the secondary bus.
  2763. * Devices on the secondary bus are left in power-on state.
  2764. */
  2765. void pci_reset_bridge_secondary_bus(struct pci_dev *dev)
  2766. {
  2767. u16 ctrl;
  2768. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  2769. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  2770. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2771. msleep(100);
  2772. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  2773. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  2774. msleep(100);
  2775. }
  2776. EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus);
  2777. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  2778. {
  2779. struct pci_dev *pdev;
  2780. if (pci_is_root_bus(dev->bus) || dev->subordinate || !dev->bus->self)
  2781. return -ENOTTY;
  2782. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2783. if (pdev != dev)
  2784. return -ENOTTY;
  2785. if (probe)
  2786. return 0;
  2787. pci_reset_bridge_secondary_bus(dev->bus->self);
  2788. return 0;
  2789. }
  2790. static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
  2791. {
  2792. int rc = -ENOTTY;
  2793. if (!hotplug || !try_module_get(hotplug->ops->owner))
  2794. return rc;
  2795. if (hotplug->ops->reset_slot)
  2796. rc = hotplug->ops->reset_slot(hotplug, probe);
  2797. module_put(hotplug->ops->owner);
  2798. return rc;
  2799. }
  2800. static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
  2801. {
  2802. struct pci_dev *pdev;
  2803. if (dev->subordinate || !dev->slot)
  2804. return -ENOTTY;
  2805. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  2806. if (pdev != dev && pdev->slot == dev->slot)
  2807. return -ENOTTY;
  2808. return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
  2809. }
  2810. static int __pci_dev_reset(struct pci_dev *dev, int probe)
  2811. {
  2812. int rc;
  2813. might_sleep();
  2814. rc = pci_dev_specific_reset(dev, probe);
  2815. if (rc != -ENOTTY)
  2816. goto done;
  2817. rc = pcie_flr(dev, probe);
  2818. if (rc != -ENOTTY)
  2819. goto done;
  2820. rc = pci_af_flr(dev, probe);
  2821. if (rc != -ENOTTY)
  2822. goto done;
  2823. rc = pci_pm_reset(dev, probe);
  2824. if (rc != -ENOTTY)
  2825. goto done;
  2826. rc = pci_dev_reset_slot_function(dev, probe);
  2827. if (rc != -ENOTTY)
  2828. goto done;
  2829. rc = pci_parent_bus_reset(dev, probe);
  2830. done:
  2831. return rc;
  2832. }
  2833. static void pci_dev_lock(struct pci_dev *dev)
  2834. {
  2835. pci_cfg_access_lock(dev);
  2836. /* block PM suspend, driver probe, etc. */
  2837. device_lock(&dev->dev);
  2838. }
  2839. static void pci_dev_unlock(struct pci_dev *dev)
  2840. {
  2841. device_unlock(&dev->dev);
  2842. pci_cfg_access_unlock(dev);
  2843. }
  2844. static void pci_dev_save_and_disable(struct pci_dev *dev)
  2845. {
  2846. /*
  2847. * Wake-up device prior to save. PM registers default to D0 after
  2848. * reset and a simple register restore doesn't reliably return
  2849. * to a non-D0 state anyway.
  2850. */
  2851. pci_set_power_state(dev, PCI_D0);
  2852. pci_save_state(dev);
  2853. /*
  2854. * Disable the device by clearing the Command register, except for
  2855. * INTx-disable which is set. This not only disables MMIO and I/O port
  2856. * BARs, but also prevents the device from being Bus Master, preventing
  2857. * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
  2858. * compliant devices, INTx-disable prevents legacy interrupts.
  2859. */
  2860. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  2861. }
  2862. static void pci_dev_restore(struct pci_dev *dev)
  2863. {
  2864. pci_restore_state(dev);
  2865. }
  2866. static int pci_dev_reset(struct pci_dev *dev, int probe)
  2867. {
  2868. int rc;
  2869. if (!probe)
  2870. pci_dev_lock(dev);
  2871. rc = __pci_dev_reset(dev, probe);
  2872. if (!probe)
  2873. pci_dev_unlock(dev);
  2874. return rc;
  2875. }
  2876. /**
  2877. * __pci_reset_function - reset a PCI device function
  2878. * @dev: PCI device to reset
  2879. *
  2880. * Some devices allow an individual function to be reset without affecting
  2881. * other functions in the same device. The PCI device must be responsive
  2882. * to PCI config space in order to use this function.
  2883. *
  2884. * The device function is presumed to be unused when this function is called.
  2885. * Resetting the device will make the contents of PCI configuration space
  2886. * random, so any caller of this must be prepared to reinitialise the
  2887. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2888. * etc.
  2889. *
  2890. * Returns 0 if the device function was successfully reset or negative if the
  2891. * device doesn't support resetting a single function.
  2892. */
  2893. int __pci_reset_function(struct pci_dev *dev)
  2894. {
  2895. return pci_dev_reset(dev, 0);
  2896. }
  2897. EXPORT_SYMBOL_GPL(__pci_reset_function);
  2898. /**
  2899. * __pci_reset_function_locked - reset a PCI device function while holding
  2900. * the @dev mutex lock.
  2901. * @dev: PCI device to reset
  2902. *
  2903. * Some devices allow an individual function to be reset without affecting
  2904. * other functions in the same device. The PCI device must be responsive
  2905. * to PCI config space in order to use this function.
  2906. *
  2907. * The device function is presumed to be unused and the caller is holding
  2908. * the device mutex lock when this function is called.
  2909. * Resetting the device will make the contents of PCI configuration space
  2910. * random, so any caller of this must be prepared to reinitialise the
  2911. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  2912. * etc.
  2913. *
  2914. * Returns 0 if the device function was successfully reset or negative if the
  2915. * device doesn't support resetting a single function.
  2916. */
  2917. int __pci_reset_function_locked(struct pci_dev *dev)
  2918. {
  2919. return __pci_dev_reset(dev, 0);
  2920. }
  2921. EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
  2922. /**
  2923. * pci_probe_reset_function - check whether the device can be safely reset
  2924. * @dev: PCI device to reset
  2925. *
  2926. * Some devices allow an individual function to be reset without affecting
  2927. * other functions in the same device. The PCI device must be responsive
  2928. * to PCI config space in order to use this function.
  2929. *
  2930. * Returns 0 if the device function can be reset or negative if the
  2931. * device doesn't support resetting a single function.
  2932. */
  2933. int pci_probe_reset_function(struct pci_dev *dev)
  2934. {
  2935. return pci_dev_reset(dev, 1);
  2936. }
  2937. /**
  2938. * pci_reset_function - quiesce and reset a PCI device function
  2939. * @dev: PCI device to reset
  2940. *
  2941. * Some devices allow an individual function to be reset without affecting
  2942. * other functions in the same device. The PCI device must be responsive
  2943. * to PCI config space in order to use this function.
  2944. *
  2945. * This function does not just reset the PCI portion of a device, but
  2946. * clears all the state associated with the device. This function differs
  2947. * from __pci_reset_function in that it saves and restores device state
  2948. * over the reset.
  2949. *
  2950. * Returns 0 if the device function was successfully reset or negative if the
  2951. * device doesn't support resetting a single function.
  2952. */
  2953. int pci_reset_function(struct pci_dev *dev)
  2954. {
  2955. int rc;
  2956. rc = pci_dev_reset(dev, 1);
  2957. if (rc)
  2958. return rc;
  2959. pci_dev_save_and_disable(dev);
  2960. rc = pci_dev_reset(dev, 0);
  2961. pci_dev_restore(dev);
  2962. return rc;
  2963. }
  2964. EXPORT_SYMBOL_GPL(pci_reset_function);
  2965. /* Lock devices from the top of the tree down */
  2966. static void pci_bus_lock(struct pci_bus *bus)
  2967. {
  2968. struct pci_dev *dev;
  2969. list_for_each_entry(dev, &bus->devices, bus_list) {
  2970. pci_dev_lock(dev);
  2971. if (dev->subordinate)
  2972. pci_bus_lock(dev->subordinate);
  2973. }
  2974. }
  2975. /* Unlock devices from the bottom of the tree up */
  2976. static void pci_bus_unlock(struct pci_bus *bus)
  2977. {
  2978. struct pci_dev *dev;
  2979. list_for_each_entry(dev, &bus->devices, bus_list) {
  2980. if (dev->subordinate)
  2981. pci_bus_unlock(dev->subordinate);
  2982. pci_dev_unlock(dev);
  2983. }
  2984. }
  2985. /* Lock devices from the top of the tree down */
  2986. static void pci_slot_lock(struct pci_slot *slot)
  2987. {
  2988. struct pci_dev *dev;
  2989. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  2990. if (!dev->slot || dev->slot != slot)
  2991. continue;
  2992. pci_dev_lock(dev);
  2993. if (dev->subordinate)
  2994. pci_bus_lock(dev->subordinate);
  2995. }
  2996. }
  2997. /* Unlock devices from the bottom of the tree up */
  2998. static void pci_slot_unlock(struct pci_slot *slot)
  2999. {
  3000. struct pci_dev *dev;
  3001. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3002. if (!dev->slot || dev->slot != slot)
  3003. continue;
  3004. if (dev->subordinate)
  3005. pci_bus_unlock(dev->subordinate);
  3006. pci_dev_unlock(dev);
  3007. }
  3008. }
  3009. /* Save and disable devices from the top of the tree down */
  3010. static void pci_bus_save_and_disable(struct pci_bus *bus)
  3011. {
  3012. struct pci_dev *dev;
  3013. list_for_each_entry(dev, &bus->devices, bus_list) {
  3014. pci_dev_save_and_disable(dev);
  3015. if (dev->subordinate)
  3016. pci_bus_save_and_disable(dev->subordinate);
  3017. }
  3018. }
  3019. /*
  3020. * Restore devices from top of the tree down - parent bridges need to be
  3021. * restored before we can get to subordinate devices.
  3022. */
  3023. static void pci_bus_restore(struct pci_bus *bus)
  3024. {
  3025. struct pci_dev *dev;
  3026. list_for_each_entry(dev, &bus->devices, bus_list) {
  3027. pci_dev_restore(dev);
  3028. if (dev->subordinate)
  3029. pci_bus_restore(dev->subordinate);
  3030. }
  3031. }
  3032. /* Save and disable devices from the top of the tree down */
  3033. static void pci_slot_save_and_disable(struct pci_slot *slot)
  3034. {
  3035. struct pci_dev *dev;
  3036. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3037. if (!dev->slot || dev->slot != slot)
  3038. continue;
  3039. pci_dev_save_and_disable(dev);
  3040. if (dev->subordinate)
  3041. pci_bus_save_and_disable(dev->subordinate);
  3042. }
  3043. }
  3044. /*
  3045. * Restore devices from top of the tree down - parent bridges need to be
  3046. * restored before we can get to subordinate devices.
  3047. */
  3048. static void pci_slot_restore(struct pci_slot *slot)
  3049. {
  3050. struct pci_dev *dev;
  3051. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  3052. if (!dev->slot || dev->slot != slot)
  3053. continue;
  3054. pci_dev_restore(dev);
  3055. if (dev->subordinate)
  3056. pci_bus_restore(dev->subordinate);
  3057. }
  3058. }
  3059. static int pci_slot_reset(struct pci_slot *slot, int probe)
  3060. {
  3061. int rc;
  3062. if (!slot)
  3063. return -ENOTTY;
  3064. if (!probe)
  3065. pci_slot_lock(slot);
  3066. might_sleep();
  3067. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  3068. if (!probe)
  3069. pci_slot_unlock(slot);
  3070. return rc;
  3071. }
  3072. /**
  3073. * pci_reset_slot - reset a PCI slot
  3074. * @slot: PCI slot to reset
  3075. *
  3076. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  3077. * independent of other slots. For instance, some slots may support slot power
  3078. * control. In the case of a 1:1 bus to slot architecture, this function may
  3079. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  3080. * Generally a slot reset should be attempted before a bus reset. All of the
  3081. * function of the slot and any subordinate buses behind the slot are reset
  3082. * through this function. PCI config space of all devices in the slot and
  3083. * behind the slot is saved before and restored after reset.
  3084. *
  3085. * Return 0 on success, non-zero on error.
  3086. */
  3087. int pci_reset_slot(struct pci_slot *slot)
  3088. {
  3089. int rc;
  3090. rc = pci_slot_reset(slot, 1);
  3091. if (rc)
  3092. return rc;
  3093. pci_slot_save_and_disable(slot);
  3094. rc = pci_slot_reset(slot, 0);
  3095. pci_slot_restore(slot);
  3096. return rc;
  3097. }
  3098. EXPORT_SYMBOL_GPL(pci_reset_slot);
  3099. static int pci_bus_reset(struct pci_bus *bus, int probe)
  3100. {
  3101. if (!bus->self)
  3102. return -ENOTTY;
  3103. if (probe)
  3104. return 0;
  3105. pci_bus_lock(bus);
  3106. might_sleep();
  3107. pci_reset_bridge_secondary_bus(bus->self);
  3108. pci_bus_unlock(bus);
  3109. return 0;
  3110. }
  3111. /**
  3112. * pci_reset_bus - reset a PCI bus
  3113. * @bus: top level PCI bus to reset
  3114. *
  3115. * Do a bus reset on the given bus and any subordinate buses, saving
  3116. * and restoring state of all devices.
  3117. *
  3118. * Return 0 on success, non-zero on error.
  3119. */
  3120. int pci_reset_bus(struct pci_bus *bus)
  3121. {
  3122. int rc;
  3123. rc = pci_bus_reset(bus, 1);
  3124. if (rc)
  3125. return rc;
  3126. pci_bus_save_and_disable(bus);
  3127. rc = pci_bus_reset(bus, 0);
  3128. pci_bus_restore(bus);
  3129. return rc;
  3130. }
  3131. EXPORT_SYMBOL_GPL(pci_reset_bus);
  3132. /**
  3133. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  3134. * @dev: PCI device to query
  3135. *
  3136. * Returns mmrbc: maximum designed memory read count in bytes
  3137. * or appropriate error value.
  3138. */
  3139. int pcix_get_max_mmrbc(struct pci_dev *dev)
  3140. {
  3141. int cap;
  3142. u32 stat;
  3143. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3144. if (!cap)
  3145. return -EINVAL;
  3146. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3147. return -EINVAL;
  3148. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  3149. }
  3150. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  3151. /**
  3152. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  3153. * @dev: PCI device to query
  3154. *
  3155. * Returns mmrbc: maximum memory read count in bytes
  3156. * or appropriate error value.
  3157. */
  3158. int pcix_get_mmrbc(struct pci_dev *dev)
  3159. {
  3160. int cap;
  3161. u16 cmd;
  3162. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3163. if (!cap)
  3164. return -EINVAL;
  3165. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3166. return -EINVAL;
  3167. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  3168. }
  3169. EXPORT_SYMBOL(pcix_get_mmrbc);
  3170. /**
  3171. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  3172. * @dev: PCI device to query
  3173. * @mmrbc: maximum memory read count in bytes
  3174. * valid values are 512, 1024, 2048, 4096
  3175. *
  3176. * If possible sets maximum memory read byte count, some bridges have erratas
  3177. * that prevent this.
  3178. */
  3179. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  3180. {
  3181. int cap;
  3182. u32 stat, v, o;
  3183. u16 cmd;
  3184. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  3185. return -EINVAL;
  3186. v = ffs(mmrbc) - 10;
  3187. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  3188. if (!cap)
  3189. return -EINVAL;
  3190. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  3191. return -EINVAL;
  3192. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  3193. return -E2BIG;
  3194. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  3195. return -EINVAL;
  3196. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  3197. if (o != v) {
  3198. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  3199. return -EIO;
  3200. cmd &= ~PCI_X_CMD_MAX_READ;
  3201. cmd |= v << 2;
  3202. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  3203. return -EIO;
  3204. }
  3205. return 0;
  3206. }
  3207. EXPORT_SYMBOL(pcix_set_mmrbc);
  3208. /**
  3209. * pcie_get_readrq - get PCI Express read request size
  3210. * @dev: PCI device to query
  3211. *
  3212. * Returns maximum memory read request in bytes
  3213. * or appropriate error value.
  3214. */
  3215. int pcie_get_readrq(struct pci_dev *dev)
  3216. {
  3217. u16 ctl;
  3218. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3219. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  3220. }
  3221. EXPORT_SYMBOL(pcie_get_readrq);
  3222. /**
  3223. * pcie_set_readrq - set PCI Express maximum memory read request
  3224. * @dev: PCI device to query
  3225. * @rq: maximum memory read count in bytes
  3226. * valid values are 128, 256, 512, 1024, 2048, 4096
  3227. *
  3228. * If possible sets maximum memory read request in bytes
  3229. */
  3230. int pcie_set_readrq(struct pci_dev *dev, int rq)
  3231. {
  3232. u16 v;
  3233. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  3234. return -EINVAL;
  3235. /*
  3236. * If using the "performance" PCIe config, we clamp the
  3237. * read rq size to the max packet size to prevent the
  3238. * host bridge generating requests larger than we can
  3239. * cope with
  3240. */
  3241. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  3242. int mps = pcie_get_mps(dev);
  3243. if (mps < 0)
  3244. return mps;
  3245. if (mps < rq)
  3246. rq = mps;
  3247. }
  3248. v = (ffs(rq) - 8) << 12;
  3249. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3250. PCI_EXP_DEVCTL_READRQ, v);
  3251. }
  3252. EXPORT_SYMBOL(pcie_set_readrq);
  3253. /**
  3254. * pcie_get_mps - get PCI Express maximum payload size
  3255. * @dev: PCI device to query
  3256. *
  3257. * Returns maximum payload size in bytes
  3258. * or appropriate error value.
  3259. */
  3260. int pcie_get_mps(struct pci_dev *dev)
  3261. {
  3262. u16 ctl;
  3263. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  3264. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  3265. }
  3266. /**
  3267. * pcie_set_mps - set PCI Express maximum payload size
  3268. * @dev: PCI device to query
  3269. * @mps: maximum payload size in bytes
  3270. * valid values are 128, 256, 512, 1024, 2048, 4096
  3271. *
  3272. * If possible sets maximum payload size
  3273. */
  3274. int pcie_set_mps(struct pci_dev *dev, int mps)
  3275. {
  3276. u16 v;
  3277. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  3278. return -EINVAL;
  3279. v = ffs(mps) - 8;
  3280. if (v > dev->pcie_mpss)
  3281. return -EINVAL;
  3282. v <<= 5;
  3283. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  3284. PCI_EXP_DEVCTL_PAYLOAD, v);
  3285. }
  3286. /**
  3287. * pci_select_bars - Make BAR mask from the type of resource
  3288. * @dev: the PCI device for which BAR mask is made
  3289. * @flags: resource type mask to be selected
  3290. *
  3291. * This helper routine makes bar mask from the type of resource.
  3292. */
  3293. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  3294. {
  3295. int i, bars = 0;
  3296. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  3297. if (pci_resource_flags(dev, i) & flags)
  3298. bars |= (1 << i);
  3299. return bars;
  3300. }
  3301. /**
  3302. * pci_resource_bar - get position of the BAR associated with a resource
  3303. * @dev: the PCI device
  3304. * @resno: the resource number
  3305. * @type: the BAR type to be filled in
  3306. *
  3307. * Returns BAR position in config space, or 0 if the BAR is invalid.
  3308. */
  3309. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  3310. {
  3311. int reg;
  3312. if (resno < PCI_ROM_RESOURCE) {
  3313. *type = pci_bar_unknown;
  3314. return PCI_BASE_ADDRESS_0 + 4 * resno;
  3315. } else if (resno == PCI_ROM_RESOURCE) {
  3316. *type = pci_bar_mem32;
  3317. return dev->rom_base_reg;
  3318. } else if (resno < PCI_BRIDGE_RESOURCES) {
  3319. /* device specific resource */
  3320. reg = pci_iov_resource_bar(dev, resno, type);
  3321. if (reg)
  3322. return reg;
  3323. }
  3324. dev_err(&dev->dev, "BAR %d: invalid resource\n", resno);
  3325. return 0;
  3326. }
  3327. /* Some architectures require additional programming to enable VGA */
  3328. static arch_set_vga_state_t arch_set_vga_state;
  3329. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  3330. {
  3331. arch_set_vga_state = func; /* NULL disables */
  3332. }
  3333. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  3334. unsigned int command_bits, u32 flags)
  3335. {
  3336. if (arch_set_vga_state)
  3337. return arch_set_vga_state(dev, decode, command_bits,
  3338. flags);
  3339. return 0;
  3340. }
  3341. /**
  3342. * pci_set_vga_state - set VGA decode state on device and parents if requested
  3343. * @dev: the PCI device
  3344. * @decode: true = enable decoding, false = disable decoding
  3345. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  3346. * @flags: traverse ancestors and change bridges
  3347. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  3348. */
  3349. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  3350. unsigned int command_bits, u32 flags)
  3351. {
  3352. struct pci_bus *bus;
  3353. struct pci_dev *bridge;
  3354. u16 cmd;
  3355. int rc;
  3356. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) & (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  3357. /* ARCH specific VGA enables */
  3358. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  3359. if (rc)
  3360. return rc;
  3361. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  3362. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3363. if (decode == true)
  3364. cmd |= command_bits;
  3365. else
  3366. cmd &= ~command_bits;
  3367. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3368. }
  3369. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  3370. return 0;
  3371. bus = dev->bus;
  3372. while (bus) {
  3373. bridge = bus->self;
  3374. if (bridge) {
  3375. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  3376. &cmd);
  3377. if (decode == true)
  3378. cmd |= PCI_BRIDGE_CTL_VGA;
  3379. else
  3380. cmd &= ~PCI_BRIDGE_CTL_VGA;
  3381. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  3382. cmd);
  3383. }
  3384. bus = bus->parent;
  3385. }
  3386. return 0;
  3387. }
  3388. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  3389. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  3390. static DEFINE_SPINLOCK(resource_alignment_lock);
  3391. /**
  3392. * pci_specified_resource_alignment - get resource alignment specified by user.
  3393. * @dev: the PCI device to get
  3394. *
  3395. * RETURNS: Resource alignment if it is specified.
  3396. * Zero if it is not specified.
  3397. */
  3398. static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev)
  3399. {
  3400. int seg, bus, slot, func, align_order, count;
  3401. resource_size_t align = 0;
  3402. char *p;
  3403. spin_lock(&resource_alignment_lock);
  3404. p = resource_alignment_param;
  3405. while (*p) {
  3406. count = 0;
  3407. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  3408. p[count] == '@') {
  3409. p += count + 1;
  3410. } else {
  3411. align_order = -1;
  3412. }
  3413. if (sscanf(p, "%x:%x:%x.%x%n",
  3414. &seg, &bus, &slot, &func, &count) != 4) {
  3415. seg = 0;
  3416. if (sscanf(p, "%x:%x.%x%n",
  3417. &bus, &slot, &func, &count) != 3) {
  3418. /* Invalid format */
  3419. printk(KERN_ERR "PCI: Can't parse resource_alignment parameter: %s\n",
  3420. p);
  3421. break;
  3422. }
  3423. }
  3424. p += count;
  3425. if (seg == pci_domain_nr(dev->bus) &&
  3426. bus == dev->bus->number &&
  3427. slot == PCI_SLOT(dev->devfn) &&
  3428. func == PCI_FUNC(dev->devfn)) {
  3429. if (align_order == -1) {
  3430. align = PAGE_SIZE;
  3431. } else {
  3432. align = 1 << align_order;
  3433. }
  3434. /* Found */
  3435. break;
  3436. }
  3437. if (*p != ';' && *p != ',') {
  3438. /* End of param or invalid format */
  3439. break;
  3440. }
  3441. p++;
  3442. }
  3443. spin_unlock(&resource_alignment_lock);
  3444. return align;
  3445. }
  3446. /*
  3447. * This function disables memory decoding and releases memory resources
  3448. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  3449. * It also rounds up size to specified alignment.
  3450. * Later on, the kernel will assign page-aligned memory resource back
  3451. * to the device.
  3452. */
  3453. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  3454. {
  3455. int i;
  3456. struct resource *r;
  3457. resource_size_t align, size;
  3458. u16 command;
  3459. /* check if specified PCI is target device to reassign */
  3460. align = pci_specified_resource_alignment(dev);
  3461. if (!align)
  3462. return;
  3463. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  3464. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  3465. dev_warn(&dev->dev,
  3466. "Can't reassign resources to host bridge.\n");
  3467. return;
  3468. }
  3469. dev_info(&dev->dev,
  3470. "Disabling memory decoding and releasing memory resources.\n");
  3471. pci_read_config_word(dev, PCI_COMMAND, &command);
  3472. command &= ~PCI_COMMAND_MEMORY;
  3473. pci_write_config_word(dev, PCI_COMMAND, command);
  3474. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  3475. r = &dev->resource[i];
  3476. if (!(r->flags & IORESOURCE_MEM))
  3477. continue;
  3478. size = resource_size(r);
  3479. if (size < align) {
  3480. size = align;
  3481. dev_info(&dev->dev,
  3482. "Rounding up size of resource #%d to %#llx.\n",
  3483. i, (unsigned long long)size);
  3484. }
  3485. r->end = size - 1;
  3486. r->start = 0;
  3487. }
  3488. /* Need to disable bridge's resource window,
  3489. * to enable the kernel to reassign new resource
  3490. * window later on.
  3491. */
  3492. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  3493. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  3494. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  3495. r = &dev->resource[i];
  3496. if (!(r->flags & IORESOURCE_MEM))
  3497. continue;
  3498. r->end = resource_size(r) - 1;
  3499. r->start = 0;
  3500. }
  3501. pci_disable_bridge_window(dev);
  3502. }
  3503. }
  3504. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  3505. {
  3506. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  3507. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  3508. spin_lock(&resource_alignment_lock);
  3509. strncpy(resource_alignment_param, buf, count);
  3510. resource_alignment_param[count] = '\0';
  3511. spin_unlock(&resource_alignment_lock);
  3512. return count;
  3513. }
  3514. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  3515. {
  3516. size_t count;
  3517. spin_lock(&resource_alignment_lock);
  3518. count = snprintf(buf, size, "%s", resource_alignment_param);
  3519. spin_unlock(&resource_alignment_lock);
  3520. return count;
  3521. }
  3522. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  3523. {
  3524. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  3525. }
  3526. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  3527. const char *buf, size_t count)
  3528. {
  3529. return pci_set_resource_alignment_param(buf, count);
  3530. }
  3531. BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  3532. pci_resource_alignment_store);
  3533. static int __init pci_resource_alignment_sysfs_init(void)
  3534. {
  3535. return bus_create_file(&pci_bus_type,
  3536. &bus_attr_resource_alignment);
  3537. }
  3538. late_initcall(pci_resource_alignment_sysfs_init);
  3539. static void pci_no_domains(void)
  3540. {
  3541. #ifdef CONFIG_PCI_DOMAINS
  3542. pci_domains_supported = 0;
  3543. #endif
  3544. }
  3545. /**
  3546. * pci_ext_cfg_avail - can we access extended PCI config space?
  3547. *
  3548. * Returns 1 if we can access PCI extended config space (offsets
  3549. * greater than 0xff). This is the default implementation. Architecture
  3550. * implementations can override this.
  3551. */
  3552. int __weak pci_ext_cfg_avail(void)
  3553. {
  3554. return 1;
  3555. }
  3556. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  3557. {
  3558. }
  3559. EXPORT_SYMBOL(pci_fixup_cardbus);
  3560. static int __init pci_setup(char *str)
  3561. {
  3562. while (str) {
  3563. char *k = strchr(str, ',');
  3564. if (k)
  3565. *k++ = 0;
  3566. if (*str && (str = pcibios_setup(str)) && *str) {
  3567. if (!strcmp(str, "nomsi")) {
  3568. pci_no_msi();
  3569. } else if (!strcmp(str, "noaer")) {
  3570. pci_no_aer();
  3571. } else if (!strncmp(str, "realloc=", 8)) {
  3572. pci_realloc_get_opt(str + 8);
  3573. } else if (!strncmp(str, "realloc", 7)) {
  3574. pci_realloc_get_opt("on");
  3575. } else if (!strcmp(str, "nodomains")) {
  3576. pci_no_domains();
  3577. } else if (!strncmp(str, "noari", 5)) {
  3578. pcie_ari_disabled = true;
  3579. } else if (!strncmp(str, "cbiosize=", 9)) {
  3580. pci_cardbus_io_size = memparse(str + 9, &str);
  3581. } else if (!strncmp(str, "cbmemsize=", 10)) {
  3582. pci_cardbus_mem_size = memparse(str + 10, &str);
  3583. } else if (!strncmp(str, "resource_alignment=", 19)) {
  3584. pci_set_resource_alignment_param(str + 19,
  3585. strlen(str + 19));
  3586. } else if (!strncmp(str, "ecrc=", 5)) {
  3587. pcie_ecrc_get_policy(str + 5);
  3588. } else if (!strncmp(str, "hpiosize=", 9)) {
  3589. pci_hotplug_io_size = memparse(str + 9, &str);
  3590. } else if (!strncmp(str, "hpmemsize=", 10)) {
  3591. pci_hotplug_mem_size = memparse(str + 10, &str);
  3592. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  3593. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  3594. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  3595. pcie_bus_config = PCIE_BUS_SAFE;
  3596. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  3597. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  3598. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  3599. pcie_bus_config = PCIE_BUS_PEER2PEER;
  3600. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  3601. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  3602. } else {
  3603. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  3604. str);
  3605. }
  3606. }
  3607. str = k;
  3608. }
  3609. return 0;
  3610. }
  3611. early_param("pci", pci_setup);
  3612. EXPORT_SYMBOL(pci_reenable_device);
  3613. EXPORT_SYMBOL(pci_enable_device_io);
  3614. EXPORT_SYMBOL(pci_enable_device_mem);
  3615. EXPORT_SYMBOL(pci_enable_device);
  3616. EXPORT_SYMBOL(pcim_enable_device);
  3617. EXPORT_SYMBOL(pcim_pin_device);
  3618. EXPORT_SYMBOL(pci_disable_device);
  3619. EXPORT_SYMBOL(pci_find_capability);
  3620. EXPORT_SYMBOL(pci_bus_find_capability);
  3621. EXPORT_SYMBOL(pci_release_regions);
  3622. EXPORT_SYMBOL(pci_request_regions);
  3623. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3624. EXPORT_SYMBOL(pci_release_region);
  3625. EXPORT_SYMBOL(pci_request_region);
  3626. EXPORT_SYMBOL(pci_request_region_exclusive);
  3627. EXPORT_SYMBOL(pci_release_selected_regions);
  3628. EXPORT_SYMBOL(pci_request_selected_regions);
  3629. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3630. EXPORT_SYMBOL(pci_set_master);
  3631. EXPORT_SYMBOL(pci_clear_master);
  3632. EXPORT_SYMBOL(pci_set_mwi);
  3633. EXPORT_SYMBOL(pci_try_set_mwi);
  3634. EXPORT_SYMBOL(pci_clear_mwi);
  3635. EXPORT_SYMBOL_GPL(pci_intx);
  3636. EXPORT_SYMBOL(pci_assign_resource);
  3637. EXPORT_SYMBOL(pci_find_parent_resource);
  3638. EXPORT_SYMBOL(pci_select_bars);
  3639. EXPORT_SYMBOL(pci_set_power_state);
  3640. EXPORT_SYMBOL(pci_save_state);
  3641. EXPORT_SYMBOL(pci_restore_state);
  3642. EXPORT_SYMBOL(pci_pme_capable);
  3643. EXPORT_SYMBOL(pci_pme_active);
  3644. EXPORT_SYMBOL(pci_wake_from_d3);
  3645. EXPORT_SYMBOL(pci_target_state);
  3646. EXPORT_SYMBOL(pci_prepare_to_sleep);
  3647. EXPORT_SYMBOL(pci_back_from_sleep);
  3648. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);