pci.c 114 KB

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