pci.c 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Bus Services, see include/linux/pci.h for further explanation.
  4. *
  5. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  6. * David Mosberger-Tang
  7. *
  8. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmi.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/pci.h>
  18. #include <linux/pm.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. #include <linux/log2.h>
  24. #include <linux/logic_pio.h>
  25. #include <linux/pm_wakeup.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/pci_hotplug.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/pci-ats.h>
  32. #include <asm/setup.h>
  33. #include <asm/dma.h>
  34. #include <linux/aer.h>
  35. #include "pci.h"
  36. DEFINE_MUTEX(pci_slot_mutex);
  37. const char *pci_power_names[] = {
  38. "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
  39. };
  40. EXPORT_SYMBOL_GPL(pci_power_names);
  41. int isa_dma_bridge_buggy;
  42. EXPORT_SYMBOL(isa_dma_bridge_buggy);
  43. int pci_pci_problems;
  44. EXPORT_SYMBOL(pci_pci_problems);
  45. unsigned int pci_pm_d3_delay;
  46. static void pci_pme_list_scan(struct work_struct *work);
  47. static LIST_HEAD(pci_pme_list);
  48. static DEFINE_MUTEX(pci_pme_list_mutex);
  49. static DECLARE_DELAYED_WORK(pci_pme_work, pci_pme_list_scan);
  50. struct pci_pme_device {
  51. struct list_head list;
  52. struct pci_dev *dev;
  53. };
  54. #define PME_TIMEOUT 1000 /* How long between PME checks */
  55. static void pci_dev_d3_sleep(struct pci_dev *dev)
  56. {
  57. unsigned int delay = dev->d3_delay;
  58. if (delay < pci_pm_d3_delay)
  59. delay = pci_pm_d3_delay;
  60. if (delay)
  61. msleep(delay);
  62. }
  63. #ifdef CONFIG_PCI_DOMAINS
  64. int pci_domains_supported = 1;
  65. #endif
  66. #define DEFAULT_CARDBUS_IO_SIZE (256)
  67. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  68. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  69. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  70. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  71. #define DEFAULT_HOTPLUG_IO_SIZE (256)
  72. #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
  73. /* pci=hpmemsize=nnM,hpiosize=nn can override this */
  74. unsigned long pci_hotplug_io_size = DEFAULT_HOTPLUG_IO_SIZE;
  75. unsigned long pci_hotplug_mem_size = DEFAULT_HOTPLUG_MEM_SIZE;
  76. #define DEFAULT_HOTPLUG_BUS_SIZE 1
  77. unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
  78. enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
  79. /*
  80. * The default CLS is used if arch didn't set CLS explicitly and not
  81. * all pci devices agree on the same value. Arch can override either
  82. * the dfl or actual value as it sees fit. Don't forget this is
  83. * measured in 32-bit words, not bytes.
  84. */
  85. u8 pci_dfl_cache_line_size = L1_CACHE_BYTES >> 2;
  86. u8 pci_cache_line_size;
  87. /*
  88. * If we set up a device for bus mastering, we need to check the latency
  89. * timer as certain BIOSes forget to set it properly.
  90. */
  91. unsigned int pcibios_max_latency = 255;
  92. /* If set, the PCIe ARI capability will not be used. */
  93. static bool pcie_ari_disabled;
  94. /* If set, the PCIe ATS capability will not be used. */
  95. static bool pcie_ats_disabled;
  96. /* If set, the PCI config space of each device is printed during boot. */
  97. bool pci_early_dump;
  98. bool pci_ats_disabled(void)
  99. {
  100. return pcie_ats_disabled;
  101. }
  102. /* Disable bridge_d3 for all PCIe ports */
  103. static bool pci_bridge_d3_disable;
  104. /* Force bridge_d3 for all PCIe ports */
  105. static bool pci_bridge_d3_force;
  106. static int __init pcie_port_pm_setup(char *str)
  107. {
  108. if (!strcmp(str, "off"))
  109. pci_bridge_d3_disable = true;
  110. else if (!strcmp(str, "force"))
  111. pci_bridge_d3_force = true;
  112. return 1;
  113. }
  114. __setup("pcie_port_pm=", pcie_port_pm_setup);
  115. /* Time to wait after a reset for device to become responsive */
  116. #define PCIE_RESET_READY_POLL_MS 60000
  117. /**
  118. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  119. * @bus: pointer to PCI bus structure to search
  120. *
  121. * Given a PCI bus, returns the highest PCI bus number present in the set
  122. * including the given PCI bus and its list of child PCI buses.
  123. */
  124. unsigned char pci_bus_max_busnr(struct pci_bus *bus)
  125. {
  126. struct pci_bus *tmp;
  127. unsigned char max, n;
  128. max = bus->busn_res.end;
  129. list_for_each_entry(tmp, &bus->children, node) {
  130. n = pci_bus_max_busnr(tmp);
  131. if (n > max)
  132. max = n;
  133. }
  134. return max;
  135. }
  136. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  137. #ifdef CONFIG_HAS_IOMEM
  138. void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
  139. {
  140. struct resource *res = &pdev->resource[bar];
  141. /*
  142. * Make sure the BAR is actually a memory resource, not an IO resource
  143. */
  144. if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
  145. pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
  146. return NULL;
  147. }
  148. return ioremap_nocache(res->start, resource_size(res));
  149. }
  150. EXPORT_SYMBOL_GPL(pci_ioremap_bar);
  151. void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
  152. {
  153. /*
  154. * Make sure the BAR is actually a memory resource, not an IO resource
  155. */
  156. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  157. WARN_ON(1);
  158. return NULL;
  159. }
  160. return ioremap_wc(pci_resource_start(pdev, bar),
  161. pci_resource_len(pdev, bar));
  162. }
  163. EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
  164. #endif
  165. /**
  166. * pci_dev_str_match_path - test if a path string matches a device
  167. * @dev: the PCI device to test
  168. * @path: string to match the device against
  169. * @endptr: pointer to the string after the match
  170. *
  171. * Test if a string (typically from a kernel parameter) formatted as a
  172. * path of device/function addresses matches a PCI device. The string must
  173. * be of the form:
  174. *
  175. * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
  176. *
  177. * A path for a device can be obtained using 'lspci -t'. Using a path
  178. * is more robust against bus renumbering than using only a single bus,
  179. * device and function address.
  180. *
  181. * Returns 1 if the string matches the device, 0 if it does not and
  182. * a negative error code if it fails to parse the string.
  183. */
  184. static int pci_dev_str_match_path(struct pci_dev *dev, const char *path,
  185. const char **endptr)
  186. {
  187. int ret;
  188. int seg, bus, slot, func;
  189. char *wpath, *p;
  190. char end;
  191. *endptr = strchrnul(path, ';');
  192. wpath = kmemdup_nul(path, *endptr - path, GFP_KERNEL);
  193. if (!wpath)
  194. return -ENOMEM;
  195. while (1) {
  196. p = strrchr(wpath, '/');
  197. if (!p)
  198. break;
  199. ret = sscanf(p, "/%x.%x%c", &slot, &func, &end);
  200. if (ret != 2) {
  201. ret = -EINVAL;
  202. goto free_and_exit;
  203. }
  204. if (dev->devfn != PCI_DEVFN(slot, func)) {
  205. ret = 0;
  206. goto free_and_exit;
  207. }
  208. /*
  209. * Note: we don't need to get a reference to the upstream
  210. * bridge because we hold a reference to the top level
  211. * device which should hold a reference to the bridge,
  212. * and so on.
  213. */
  214. dev = pci_upstream_bridge(dev);
  215. if (!dev) {
  216. ret = 0;
  217. goto free_and_exit;
  218. }
  219. *p = 0;
  220. }
  221. ret = sscanf(wpath, "%x:%x:%x.%x%c", &seg, &bus, &slot,
  222. &func, &end);
  223. if (ret != 4) {
  224. seg = 0;
  225. ret = sscanf(wpath, "%x:%x.%x%c", &bus, &slot, &func, &end);
  226. if (ret != 3) {
  227. ret = -EINVAL;
  228. goto free_and_exit;
  229. }
  230. }
  231. ret = (seg == pci_domain_nr(dev->bus) &&
  232. bus == dev->bus->number &&
  233. dev->devfn == PCI_DEVFN(slot, func));
  234. free_and_exit:
  235. kfree(wpath);
  236. return ret;
  237. }
  238. /**
  239. * pci_dev_str_match - test if a string matches a device
  240. * @dev: the PCI device to test
  241. * @p: string to match the device against
  242. * @endptr: pointer to the string after the match
  243. *
  244. * Test if a string (typically from a kernel parameter) matches a specified
  245. * PCI device. The string may be of one of the following formats:
  246. *
  247. * [<domain>:]<bus>:<device>.<func>[/<device>.<func>]*
  248. * pci:<vendor>:<device>[:<subvendor>:<subdevice>]
  249. *
  250. * The first format specifies a PCI bus/device/function address which
  251. * may change if new hardware is inserted, if motherboard firmware changes,
  252. * or due to changes caused in kernel parameters. If the domain is
  253. * left unspecified, it is taken to be 0. In order to be robust against
  254. * bus renumbering issues, a path of PCI device/function numbers may be used
  255. * to address the specific device. The path for a device can be determined
  256. * through the use of 'lspci -t'.
  257. *
  258. * The second format matches devices using IDs in the configuration
  259. * space which may match multiple devices in the system. A value of 0
  260. * for any field will match all devices. (Note: this differs from
  261. * in-kernel code that uses PCI_ANY_ID which is ~0; this is for
  262. * legacy reasons and convenience so users don't have to specify
  263. * FFFFFFFFs on the command line.)
  264. *
  265. * Returns 1 if the string matches the device, 0 if it does not and
  266. * a negative error code if the string cannot be parsed.
  267. */
  268. static int pci_dev_str_match(struct pci_dev *dev, const char *p,
  269. const char **endptr)
  270. {
  271. int ret;
  272. int count;
  273. unsigned short vendor, device, subsystem_vendor, subsystem_device;
  274. if (strncmp(p, "pci:", 4) == 0) {
  275. /* PCI vendor/device (subvendor/subdevice) IDs are specified */
  276. p += 4;
  277. ret = sscanf(p, "%hx:%hx:%hx:%hx%n", &vendor, &device,
  278. &subsystem_vendor, &subsystem_device, &count);
  279. if (ret != 4) {
  280. ret = sscanf(p, "%hx:%hx%n", &vendor, &device, &count);
  281. if (ret != 2)
  282. return -EINVAL;
  283. subsystem_vendor = 0;
  284. subsystem_device = 0;
  285. }
  286. p += count;
  287. if ((!vendor || vendor == dev->vendor) &&
  288. (!device || device == dev->device) &&
  289. (!subsystem_vendor ||
  290. subsystem_vendor == dev->subsystem_vendor) &&
  291. (!subsystem_device ||
  292. subsystem_device == dev->subsystem_device))
  293. goto found;
  294. } else {
  295. /*
  296. * PCI Bus, Device, Function IDs are specified
  297. * (optionally, may include a path of devfns following it)
  298. */
  299. ret = pci_dev_str_match_path(dev, p, &p);
  300. if (ret < 0)
  301. return ret;
  302. else if (ret)
  303. goto found;
  304. }
  305. *endptr = p;
  306. return 0;
  307. found:
  308. *endptr = p;
  309. return 1;
  310. }
  311. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  312. u8 pos, int cap, int *ttl)
  313. {
  314. u8 id;
  315. u16 ent;
  316. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  317. while ((*ttl)--) {
  318. if (pos < 0x40)
  319. break;
  320. pos &= ~3;
  321. pci_bus_read_config_word(bus, devfn, pos, &ent);
  322. id = ent & 0xff;
  323. if (id == 0xff)
  324. break;
  325. if (id == cap)
  326. return pos;
  327. pos = (ent >> 8);
  328. }
  329. return 0;
  330. }
  331. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  332. u8 pos, int cap)
  333. {
  334. int ttl = PCI_FIND_CAP_TTL;
  335. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  336. }
  337. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  338. {
  339. return __pci_find_next_cap(dev->bus, dev->devfn,
  340. pos + PCI_CAP_LIST_NEXT, cap);
  341. }
  342. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  343. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  344. unsigned int devfn, u8 hdr_type)
  345. {
  346. u16 status;
  347. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  348. if (!(status & PCI_STATUS_CAP_LIST))
  349. return 0;
  350. switch (hdr_type) {
  351. case PCI_HEADER_TYPE_NORMAL:
  352. case PCI_HEADER_TYPE_BRIDGE:
  353. return PCI_CAPABILITY_LIST;
  354. case PCI_HEADER_TYPE_CARDBUS:
  355. return PCI_CB_CAPABILITY_LIST;
  356. }
  357. return 0;
  358. }
  359. /**
  360. * pci_find_capability - query for devices' capabilities
  361. * @dev: PCI device to query
  362. * @cap: capability code
  363. *
  364. * Tell if a device supports a given PCI capability.
  365. * Returns the address of the requested capability structure within the
  366. * device's PCI configuration space or 0 in case the device does not
  367. * support it. Possible values for @cap:
  368. *
  369. * %PCI_CAP_ID_PM Power Management
  370. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  371. * %PCI_CAP_ID_VPD Vital Product Data
  372. * %PCI_CAP_ID_SLOTID Slot Identification
  373. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  374. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  375. * %PCI_CAP_ID_PCIX PCI-X
  376. * %PCI_CAP_ID_EXP PCI Express
  377. */
  378. int pci_find_capability(struct pci_dev *dev, int cap)
  379. {
  380. int pos;
  381. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  382. if (pos)
  383. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  384. return pos;
  385. }
  386. EXPORT_SYMBOL(pci_find_capability);
  387. /**
  388. * pci_bus_find_capability - query for devices' capabilities
  389. * @bus: the PCI bus to query
  390. * @devfn: PCI device to query
  391. * @cap: capability code
  392. *
  393. * Like pci_find_capability() but works for pci devices that do not have a
  394. * pci_dev structure set up yet.
  395. *
  396. * Returns the address of the requested capability structure within the
  397. * device's PCI configuration space or 0 in case the device does not
  398. * support it.
  399. */
  400. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  401. {
  402. int pos;
  403. u8 hdr_type;
  404. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  405. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  406. if (pos)
  407. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  408. return pos;
  409. }
  410. EXPORT_SYMBOL(pci_bus_find_capability);
  411. /**
  412. * pci_find_next_ext_capability - Find an extended capability
  413. * @dev: PCI device to query
  414. * @start: address at which to start looking (0 to start at beginning of list)
  415. * @cap: capability code
  416. *
  417. * Returns the address of the next matching extended capability structure
  418. * within the device's PCI configuration space or 0 if the device does
  419. * not support it. Some capabilities can occur several times, e.g., the
  420. * vendor-specific capability, and this provides a way to find them all.
  421. */
  422. int pci_find_next_ext_capability(struct pci_dev *dev, int start, int cap)
  423. {
  424. u32 header;
  425. int ttl;
  426. int pos = PCI_CFG_SPACE_SIZE;
  427. /* minimum 8 bytes per capability */
  428. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  429. if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
  430. return 0;
  431. if (start)
  432. pos = start;
  433. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  434. return 0;
  435. /*
  436. * If we have no capabilities, this is indicated by cap ID,
  437. * cap version and next pointer all being 0.
  438. */
  439. if (header == 0)
  440. return 0;
  441. while (ttl-- > 0) {
  442. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  443. return pos;
  444. pos = PCI_EXT_CAP_NEXT(header);
  445. if (pos < PCI_CFG_SPACE_SIZE)
  446. break;
  447. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  448. break;
  449. }
  450. return 0;
  451. }
  452. EXPORT_SYMBOL_GPL(pci_find_next_ext_capability);
  453. /**
  454. * pci_find_ext_capability - Find an extended capability
  455. * @dev: PCI device to query
  456. * @cap: capability code
  457. *
  458. * Returns the address of the requested extended capability structure
  459. * within the device's PCI configuration space or 0 if the device does
  460. * not support it. Possible values for @cap:
  461. *
  462. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  463. * %PCI_EXT_CAP_ID_VC Virtual Channel
  464. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  465. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  466. */
  467. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  468. {
  469. return pci_find_next_ext_capability(dev, 0, cap);
  470. }
  471. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  472. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  473. {
  474. int rc, ttl = PCI_FIND_CAP_TTL;
  475. u8 cap, mask;
  476. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  477. mask = HT_3BIT_CAP_MASK;
  478. else
  479. mask = HT_5BIT_CAP_MASK;
  480. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  481. PCI_CAP_ID_HT, &ttl);
  482. while (pos) {
  483. rc = pci_read_config_byte(dev, pos + 3, &cap);
  484. if (rc != PCIBIOS_SUCCESSFUL)
  485. return 0;
  486. if ((cap & mask) == ht_cap)
  487. return pos;
  488. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  489. pos + PCI_CAP_LIST_NEXT,
  490. PCI_CAP_ID_HT, &ttl);
  491. }
  492. return 0;
  493. }
  494. /**
  495. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  496. * @dev: PCI device to query
  497. * @pos: Position from which to continue searching
  498. * @ht_cap: Hypertransport capability code
  499. *
  500. * To be used in conjunction with pci_find_ht_capability() to search for
  501. * all capabilities matching @ht_cap. @pos should always be a value returned
  502. * from pci_find_ht_capability().
  503. *
  504. * NB. To be 100% safe against broken PCI devices, the caller should take
  505. * steps to avoid an infinite loop.
  506. */
  507. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  508. {
  509. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  510. }
  511. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  512. /**
  513. * pci_find_ht_capability - query a device's Hypertransport capabilities
  514. * @dev: PCI device to query
  515. * @ht_cap: Hypertransport capability code
  516. *
  517. * Tell if a device supports a given Hypertransport capability.
  518. * Returns an address within the device's PCI configuration space
  519. * or 0 in case the device does not support the request capability.
  520. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  521. * which has a Hypertransport capability matching @ht_cap.
  522. */
  523. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  524. {
  525. int pos;
  526. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  527. if (pos)
  528. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  529. return pos;
  530. }
  531. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  532. /**
  533. * pci_find_parent_resource - return resource region of parent bus of given region
  534. * @dev: PCI device structure contains resources to be searched
  535. * @res: child resource record for which parent is sought
  536. *
  537. * For given resource region of given device, return the resource
  538. * region of parent bus the given region is contained in.
  539. */
  540. struct resource *pci_find_parent_resource(const struct pci_dev *dev,
  541. struct resource *res)
  542. {
  543. const struct pci_bus *bus = dev->bus;
  544. struct resource *r;
  545. int i;
  546. pci_bus_for_each_resource(bus, r, i) {
  547. if (!r)
  548. continue;
  549. if (resource_contains(r, res)) {
  550. /*
  551. * If the window is prefetchable but the BAR is
  552. * not, the allocator made a mistake.
  553. */
  554. if (r->flags & IORESOURCE_PREFETCH &&
  555. !(res->flags & IORESOURCE_PREFETCH))
  556. return NULL;
  557. /*
  558. * If we're below a transparent bridge, there may
  559. * be both a positively-decoded aperture and a
  560. * subtractively-decoded region that contain the BAR.
  561. * We want the positively-decoded one, so this depends
  562. * on pci_bus_for_each_resource() giving us those
  563. * first.
  564. */
  565. return r;
  566. }
  567. }
  568. return NULL;
  569. }
  570. EXPORT_SYMBOL(pci_find_parent_resource);
  571. /**
  572. * pci_find_resource - Return matching PCI device resource
  573. * @dev: PCI device to query
  574. * @res: Resource to look for
  575. *
  576. * Goes over standard PCI resources (BARs) and checks if the given resource
  577. * is partially or fully contained in any of them. In that case the
  578. * matching resource is returned, %NULL otherwise.
  579. */
  580. struct resource *pci_find_resource(struct pci_dev *dev, struct resource *res)
  581. {
  582. int i;
  583. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  584. struct resource *r = &dev->resource[i];
  585. if (r->start && resource_contains(r, res))
  586. return r;
  587. }
  588. return NULL;
  589. }
  590. EXPORT_SYMBOL(pci_find_resource);
  591. /**
  592. * pci_find_pcie_root_port - return PCIe Root Port
  593. * @dev: PCI device to query
  594. *
  595. * Traverse up the parent chain and return the PCIe Root Port PCI Device
  596. * for a given PCI Device.
  597. */
  598. struct pci_dev *pci_find_pcie_root_port(struct pci_dev *dev)
  599. {
  600. struct pci_dev *bridge, *highest_pcie_bridge = dev;
  601. bridge = pci_upstream_bridge(dev);
  602. while (bridge && pci_is_pcie(bridge)) {
  603. highest_pcie_bridge = bridge;
  604. bridge = pci_upstream_bridge(bridge);
  605. }
  606. if (pci_pcie_type(highest_pcie_bridge) != PCI_EXP_TYPE_ROOT_PORT)
  607. return NULL;
  608. return highest_pcie_bridge;
  609. }
  610. EXPORT_SYMBOL(pci_find_pcie_root_port);
  611. /**
  612. * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
  613. * @dev: the PCI device to operate on
  614. * @pos: config space offset of status word
  615. * @mask: mask of bit(s) to care about in status word
  616. *
  617. * Return 1 when mask bit(s) in status word clear, 0 otherwise.
  618. */
  619. int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
  620. {
  621. int i;
  622. /* Wait for Transaction Pending bit clean */
  623. for (i = 0; i < 4; i++) {
  624. u16 status;
  625. if (i)
  626. msleep((1 << (i - 1)) * 100);
  627. pci_read_config_word(dev, pos, &status);
  628. if (!(status & mask))
  629. return 1;
  630. }
  631. return 0;
  632. }
  633. /**
  634. * pci_restore_bars - restore a device's BAR values (e.g. after wake-up)
  635. * @dev: PCI device to have its BARs restored
  636. *
  637. * Restore the BAR values for a given device, so as to make it
  638. * accessible by its driver.
  639. */
  640. static void pci_restore_bars(struct pci_dev *dev)
  641. {
  642. int i;
  643. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
  644. pci_update_resource(dev, i);
  645. }
  646. static const struct pci_platform_pm_ops *pci_platform_pm;
  647. int pci_set_platform_pm(const struct pci_platform_pm_ops *ops)
  648. {
  649. if (!ops->is_manageable || !ops->set_state || !ops->get_state ||
  650. !ops->choose_state || !ops->set_wakeup || !ops->need_resume)
  651. return -EINVAL;
  652. pci_platform_pm = ops;
  653. return 0;
  654. }
  655. static inline bool platform_pci_power_manageable(struct pci_dev *dev)
  656. {
  657. return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
  658. }
  659. static inline int platform_pci_set_power_state(struct pci_dev *dev,
  660. pci_power_t t)
  661. {
  662. return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
  663. }
  664. static inline pci_power_t platform_pci_get_power_state(struct pci_dev *dev)
  665. {
  666. return pci_platform_pm ? pci_platform_pm->get_state(dev) : PCI_UNKNOWN;
  667. }
  668. static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
  669. {
  670. return pci_platform_pm ?
  671. pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
  672. }
  673. static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable)
  674. {
  675. return pci_platform_pm ?
  676. pci_platform_pm->set_wakeup(dev, enable) : -ENODEV;
  677. }
  678. static inline bool platform_pci_need_resume(struct pci_dev *dev)
  679. {
  680. return pci_platform_pm ? pci_platform_pm->need_resume(dev) : false;
  681. }
  682. static inline bool platform_pci_bridge_d3(struct pci_dev *dev)
  683. {
  684. return pci_platform_pm ? pci_platform_pm->bridge_d3(dev) : false;
  685. }
  686. /**
  687. * pci_raw_set_power_state - Use PCI PM registers to set the power state of
  688. * given PCI device
  689. * @dev: PCI device to handle.
  690. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  691. *
  692. * RETURN VALUE:
  693. * -EINVAL if the requested state is invalid.
  694. * -EIO if device does not support PCI PM or its PM capabilities register has a
  695. * wrong version, or device doesn't support the requested state.
  696. * 0 if device already is in the requested state.
  697. * 0 if device's power state has been successfully changed.
  698. */
  699. static int pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
  700. {
  701. u16 pmcsr;
  702. bool need_restore = false;
  703. /* Check if we're already there */
  704. if (dev->current_state == state)
  705. return 0;
  706. if (!dev->pm_cap)
  707. return -EIO;
  708. if (state < PCI_D0 || state > PCI_D3hot)
  709. return -EINVAL;
  710. /* Validate current state:
  711. * Can enter D0 from any state, but if we can only go deeper
  712. * to sleep if we're already in a low power state
  713. */
  714. if (state != PCI_D0 && dev->current_state <= PCI_D3cold
  715. && dev->current_state > state) {
  716. pci_err(dev, "invalid power transition (from state %d to %d)\n",
  717. dev->current_state, state);
  718. return -EINVAL;
  719. }
  720. /* check if this device supports the desired state */
  721. if ((state == PCI_D1 && !dev->d1_support)
  722. || (state == PCI_D2 && !dev->d2_support))
  723. return -EIO;
  724. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  725. /* If we're (effectively) in D3, force entire word to 0.
  726. * This doesn't affect PME_Status, disables PME_En, and
  727. * sets PowerState to 0.
  728. */
  729. switch (dev->current_state) {
  730. case PCI_D0:
  731. case PCI_D1:
  732. case PCI_D2:
  733. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  734. pmcsr |= state;
  735. break;
  736. case PCI_D3hot:
  737. case PCI_D3cold:
  738. case PCI_UNKNOWN: /* Boot-up */
  739. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  740. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  741. need_restore = true;
  742. /* Fall-through: force to D0 */
  743. default:
  744. pmcsr = 0;
  745. break;
  746. }
  747. /* enter specified state */
  748. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  749. /* Mandatory power management transition delays */
  750. /* see PCI PM 1.1 5.6.1 table 18 */
  751. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  752. pci_dev_d3_sleep(dev);
  753. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  754. udelay(PCI_PM_D2_DELAY);
  755. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  756. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  757. if (dev->current_state != state && printk_ratelimit())
  758. pci_info(dev, "Refused to change power state, currently in D%d\n",
  759. dev->current_state);
  760. /*
  761. * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  762. * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  763. * from D3hot to D0 _may_ perform an internal reset, thereby
  764. * going to "D0 Uninitialized" rather than "D0 Initialized".
  765. * For example, at least some versions of the 3c905B and the
  766. * 3c556B exhibit this behaviour.
  767. *
  768. * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  769. * devices in a D3hot state at boot. Consequently, we need to
  770. * restore at least the BARs so that the device will be
  771. * accessible to its driver.
  772. */
  773. if (need_restore)
  774. pci_restore_bars(dev);
  775. if (dev->bus->self)
  776. pcie_aspm_pm_state_change(dev->bus->self);
  777. return 0;
  778. }
  779. /**
  780. * pci_update_current_state - Read power state of given device and cache it
  781. * @dev: PCI device to handle.
  782. * @state: State to cache in case the device doesn't have the PM capability
  783. *
  784. * The power state is read from the PMCSR register, which however is
  785. * inaccessible in D3cold. The platform firmware is therefore queried first
  786. * to detect accessibility of the register. In case the platform firmware
  787. * reports an incorrect state or the device isn't power manageable by the
  788. * platform at all, we try to detect D3cold by testing accessibility of the
  789. * vendor ID in config space.
  790. */
  791. void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
  792. {
  793. if (platform_pci_get_power_state(dev) == PCI_D3cold ||
  794. !pci_device_is_present(dev)) {
  795. dev->current_state = PCI_D3cold;
  796. } else if (dev->pm_cap) {
  797. u16 pmcsr;
  798. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  799. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  800. } else {
  801. dev->current_state = state;
  802. }
  803. }
  804. /**
  805. * pci_power_up - Put the given device into D0 forcibly
  806. * @dev: PCI device to power up
  807. */
  808. void pci_power_up(struct pci_dev *dev)
  809. {
  810. if (platform_pci_power_manageable(dev))
  811. platform_pci_set_power_state(dev, PCI_D0);
  812. pci_raw_set_power_state(dev, PCI_D0);
  813. pci_update_current_state(dev, PCI_D0);
  814. }
  815. /**
  816. * pci_platform_power_transition - Use platform to change device power state
  817. * @dev: PCI device to handle.
  818. * @state: State to put the device into.
  819. */
  820. static int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
  821. {
  822. int error;
  823. if (platform_pci_power_manageable(dev)) {
  824. error = platform_pci_set_power_state(dev, state);
  825. if (!error)
  826. pci_update_current_state(dev, state);
  827. } else
  828. error = -ENODEV;
  829. if (error && !dev->pm_cap) /* Fall back to PCI_D0 */
  830. dev->current_state = PCI_D0;
  831. return error;
  832. }
  833. /**
  834. * pci_wakeup - Wake up a PCI device
  835. * @pci_dev: Device to handle.
  836. * @ign: ignored parameter
  837. */
  838. static int pci_wakeup(struct pci_dev *pci_dev, void *ign)
  839. {
  840. pci_wakeup_event(pci_dev);
  841. pm_request_resume(&pci_dev->dev);
  842. return 0;
  843. }
  844. /**
  845. * pci_wakeup_bus - Walk given bus and wake up devices on it
  846. * @bus: Top bus of the subtree to walk.
  847. */
  848. void pci_wakeup_bus(struct pci_bus *bus)
  849. {
  850. if (bus)
  851. pci_walk_bus(bus, pci_wakeup, NULL);
  852. }
  853. /**
  854. * __pci_start_power_transition - Start power transition of a PCI device
  855. * @dev: PCI device to handle.
  856. * @state: State to put the device into.
  857. */
  858. static void __pci_start_power_transition(struct pci_dev *dev, pci_power_t state)
  859. {
  860. if (state == PCI_D0) {
  861. pci_platform_power_transition(dev, PCI_D0);
  862. /*
  863. * Mandatory power management transition delays, see
  864. * PCI Express Base Specification Revision 2.0 Section
  865. * 6.6.1: Conventional Reset. Do not delay for
  866. * devices powered on/off by corresponding bridge,
  867. * because have already delayed for the bridge.
  868. */
  869. if (dev->runtime_d3cold) {
  870. if (dev->d3cold_delay && !dev->imm_ready)
  871. msleep(dev->d3cold_delay);
  872. /*
  873. * When powering on a bridge from D3cold, the
  874. * whole hierarchy may be powered on into
  875. * D0uninitialized state, resume them to give
  876. * them a chance to suspend again
  877. */
  878. pci_wakeup_bus(dev->subordinate);
  879. }
  880. }
  881. }
  882. /**
  883. * __pci_dev_set_current_state - Set current state of a PCI device
  884. * @dev: Device to handle
  885. * @data: pointer to state to be set
  886. */
  887. static int __pci_dev_set_current_state(struct pci_dev *dev, void *data)
  888. {
  889. pci_power_t state = *(pci_power_t *)data;
  890. dev->current_state = state;
  891. return 0;
  892. }
  893. /**
  894. * pci_bus_set_current_state - Walk given bus and set current state of devices
  895. * @bus: Top bus of the subtree to walk.
  896. * @state: state to be set
  897. */
  898. void pci_bus_set_current_state(struct pci_bus *bus, pci_power_t state)
  899. {
  900. if (bus)
  901. pci_walk_bus(bus, __pci_dev_set_current_state, &state);
  902. }
  903. /**
  904. * __pci_complete_power_transition - Complete power transition of a PCI device
  905. * @dev: PCI device to handle.
  906. * @state: State to put the device into.
  907. *
  908. * This function should not be called directly by device drivers.
  909. */
  910. int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
  911. {
  912. int ret;
  913. if (state <= PCI_D0)
  914. return -EINVAL;
  915. ret = pci_platform_power_transition(dev, state);
  916. /* Power off the bridge may power off the whole hierarchy */
  917. if (!ret && state == PCI_D3cold)
  918. pci_bus_set_current_state(dev->subordinate, PCI_D3cold);
  919. return ret;
  920. }
  921. EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
  922. /**
  923. * pci_set_power_state - Set the power state of a PCI device
  924. * @dev: PCI device to handle.
  925. * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
  926. *
  927. * Transition a device to a new power state, using the platform firmware and/or
  928. * the device's PCI PM registers.
  929. *
  930. * RETURN VALUE:
  931. * -EINVAL if the requested state is invalid.
  932. * -EIO if device does not support PCI PM or its PM capabilities register has a
  933. * wrong version, or device doesn't support the requested state.
  934. * 0 if the transition is to D1 or D2 but D1 and D2 are not supported.
  935. * 0 if device already is in the requested state.
  936. * 0 if the transition is to D3 but D3 is not supported.
  937. * 0 if device's power state has been successfully changed.
  938. */
  939. int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  940. {
  941. int error;
  942. /* bound the state we're entering */
  943. if (state > PCI_D3cold)
  944. state = PCI_D3cold;
  945. else if (state < PCI_D0)
  946. state = PCI_D0;
  947. else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  948. /*
  949. * If the device or the parent bridge do not support PCI PM,
  950. * ignore the request if we're doing anything other than putting
  951. * it into D0 (which would only happen on boot).
  952. */
  953. return 0;
  954. /* Check if we're already there */
  955. if (dev->current_state == state)
  956. return 0;
  957. __pci_start_power_transition(dev, state);
  958. /* This device is quirked not to be put into D3, so
  959. don't put it in D3 */
  960. if (state >= PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
  961. return 0;
  962. /*
  963. * To put device in D3cold, we put device into D3hot in native
  964. * way, then put device into D3cold with platform ops
  965. */
  966. error = pci_raw_set_power_state(dev, state > PCI_D3hot ?
  967. PCI_D3hot : state);
  968. if (!__pci_complete_power_transition(dev, state))
  969. error = 0;
  970. return error;
  971. }
  972. EXPORT_SYMBOL(pci_set_power_state);
  973. /**
  974. * pci_choose_state - Choose the power state of a PCI device
  975. * @dev: PCI device to be suspended
  976. * @state: target sleep state for the whole system. This is the value
  977. * that is passed to suspend() function.
  978. *
  979. * Returns PCI power state suitable for given device and given system
  980. * message.
  981. */
  982. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  983. {
  984. pci_power_t ret;
  985. if (!dev->pm_cap)
  986. return PCI_D0;
  987. ret = platform_pci_choose_state(dev);
  988. if (ret != PCI_POWER_ERROR)
  989. return ret;
  990. switch (state.event) {
  991. case PM_EVENT_ON:
  992. return PCI_D0;
  993. case PM_EVENT_FREEZE:
  994. case PM_EVENT_PRETHAW:
  995. /* REVISIT both freeze and pre-thaw "should" use D0 */
  996. case PM_EVENT_SUSPEND:
  997. case PM_EVENT_HIBERNATE:
  998. return PCI_D3hot;
  999. default:
  1000. pci_info(dev, "unrecognized suspend event %d\n",
  1001. state.event);
  1002. BUG();
  1003. }
  1004. return PCI_D0;
  1005. }
  1006. EXPORT_SYMBOL(pci_choose_state);
  1007. #define PCI_EXP_SAVE_REGS 7
  1008. static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
  1009. u16 cap, bool extended)
  1010. {
  1011. struct pci_cap_saved_state *tmp;
  1012. hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
  1013. if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
  1014. return tmp;
  1015. }
  1016. return NULL;
  1017. }
  1018. struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
  1019. {
  1020. return _pci_find_saved_cap(dev, cap, false);
  1021. }
  1022. struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
  1023. {
  1024. return _pci_find_saved_cap(dev, cap, true);
  1025. }
  1026. static int pci_save_pcie_state(struct pci_dev *dev)
  1027. {
  1028. int i = 0;
  1029. struct pci_cap_saved_state *save_state;
  1030. u16 *cap;
  1031. if (!pci_is_pcie(dev))
  1032. return 0;
  1033. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1034. if (!save_state) {
  1035. pci_err(dev, "buffer not found in %s\n", __func__);
  1036. return -ENOMEM;
  1037. }
  1038. cap = (u16 *)&save_state->cap.data[0];
  1039. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &cap[i++]);
  1040. pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &cap[i++]);
  1041. pcie_capability_read_word(dev, PCI_EXP_SLTCTL, &cap[i++]);
  1042. pcie_capability_read_word(dev, PCI_EXP_RTCTL, &cap[i++]);
  1043. pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &cap[i++]);
  1044. pcie_capability_read_word(dev, PCI_EXP_LNKCTL2, &cap[i++]);
  1045. pcie_capability_read_word(dev, PCI_EXP_SLTCTL2, &cap[i++]);
  1046. return 0;
  1047. }
  1048. static void pci_restore_pcie_state(struct pci_dev *dev)
  1049. {
  1050. int i = 0;
  1051. struct pci_cap_saved_state *save_state;
  1052. u16 *cap;
  1053. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  1054. if (!save_state)
  1055. return;
  1056. cap = (u16 *)&save_state->cap.data[0];
  1057. pcie_capability_write_word(dev, PCI_EXP_DEVCTL, cap[i++]);
  1058. pcie_capability_write_word(dev, PCI_EXP_LNKCTL, cap[i++]);
  1059. pcie_capability_write_word(dev, PCI_EXP_SLTCTL, cap[i++]);
  1060. pcie_capability_write_word(dev, PCI_EXP_RTCTL, cap[i++]);
  1061. pcie_capability_write_word(dev, PCI_EXP_DEVCTL2, cap[i++]);
  1062. pcie_capability_write_word(dev, PCI_EXP_LNKCTL2, cap[i++]);
  1063. pcie_capability_write_word(dev, PCI_EXP_SLTCTL2, cap[i++]);
  1064. }
  1065. static int pci_save_pcix_state(struct pci_dev *dev)
  1066. {
  1067. int pos;
  1068. struct pci_cap_saved_state *save_state;
  1069. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1070. if (!pos)
  1071. return 0;
  1072. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1073. if (!save_state) {
  1074. pci_err(dev, "buffer not found in %s\n", __func__);
  1075. return -ENOMEM;
  1076. }
  1077. pci_read_config_word(dev, pos + PCI_X_CMD,
  1078. (u16 *)save_state->cap.data);
  1079. return 0;
  1080. }
  1081. static void pci_restore_pcix_state(struct pci_dev *dev)
  1082. {
  1083. int i = 0, pos;
  1084. struct pci_cap_saved_state *save_state;
  1085. u16 *cap;
  1086. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  1087. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1088. if (!save_state || !pos)
  1089. return;
  1090. cap = (u16 *)&save_state->cap.data[0];
  1091. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  1092. }
  1093. /**
  1094. * pci_save_state - save the PCI configuration space of a device before suspending
  1095. * @dev: - PCI device that we're dealing with
  1096. */
  1097. int pci_save_state(struct pci_dev *dev)
  1098. {
  1099. int i;
  1100. /* XXX: 100% dword access ok here? */
  1101. for (i = 0; i < 16; i++)
  1102. pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
  1103. dev->state_saved = true;
  1104. i = pci_save_pcie_state(dev);
  1105. if (i != 0)
  1106. return i;
  1107. i = pci_save_pcix_state(dev);
  1108. if (i != 0)
  1109. return i;
  1110. pci_save_dpc_state(dev);
  1111. return pci_save_vc_state(dev);
  1112. }
  1113. EXPORT_SYMBOL(pci_save_state);
  1114. static void pci_restore_config_dword(struct pci_dev *pdev, int offset,
  1115. u32 saved_val, int retry, bool force)
  1116. {
  1117. u32 val;
  1118. pci_read_config_dword(pdev, offset, &val);
  1119. if (!force && val == saved_val)
  1120. return;
  1121. for (;;) {
  1122. pci_dbg(pdev, "restoring config space at offset %#x (was %#x, writing %#x)\n",
  1123. offset, val, saved_val);
  1124. pci_write_config_dword(pdev, offset, saved_val);
  1125. if (retry-- <= 0)
  1126. return;
  1127. pci_read_config_dword(pdev, offset, &val);
  1128. if (val == saved_val)
  1129. return;
  1130. mdelay(1);
  1131. }
  1132. }
  1133. static void pci_restore_config_space_range(struct pci_dev *pdev,
  1134. int start, int end, int retry,
  1135. bool force)
  1136. {
  1137. int index;
  1138. for (index = end; index >= start; index--)
  1139. pci_restore_config_dword(pdev, 4 * index,
  1140. pdev->saved_config_space[index],
  1141. retry, force);
  1142. }
  1143. static void pci_restore_config_space(struct pci_dev *pdev)
  1144. {
  1145. if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  1146. pci_restore_config_space_range(pdev, 10, 15, 0, false);
  1147. /* Restore BARs before the command register. */
  1148. pci_restore_config_space_range(pdev, 4, 9, 10, false);
  1149. pci_restore_config_space_range(pdev, 0, 3, 0, false);
  1150. } else if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1151. pci_restore_config_space_range(pdev, 12, 15, 0, false);
  1152. /*
  1153. * Force rewriting of prefetch registers to avoid S3 resume
  1154. * issues on Intel PCI bridges that occur when these
  1155. * registers are not explicitly written.
  1156. */
  1157. pci_restore_config_space_range(pdev, 9, 11, 0, true);
  1158. pci_restore_config_space_range(pdev, 0, 8, 0, false);
  1159. } else {
  1160. pci_restore_config_space_range(pdev, 0, 15, 0, false);
  1161. }
  1162. }
  1163. static void pci_restore_rebar_state(struct pci_dev *pdev)
  1164. {
  1165. unsigned int pos, nbars, i;
  1166. u32 ctrl;
  1167. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  1168. if (!pos)
  1169. return;
  1170. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1171. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  1172. PCI_REBAR_CTRL_NBAR_SHIFT;
  1173. for (i = 0; i < nbars; i++, pos += 8) {
  1174. struct resource *res;
  1175. int bar_idx, size;
  1176. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  1177. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  1178. res = pdev->resource + bar_idx;
  1179. size = order_base_2((resource_size(res) >> 20) | 1) - 1;
  1180. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  1181. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  1182. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  1183. }
  1184. }
  1185. /**
  1186. * pci_restore_state - Restore the saved state of a PCI device
  1187. * @dev: - PCI device that we're dealing with
  1188. */
  1189. void pci_restore_state(struct pci_dev *dev)
  1190. {
  1191. if (!dev->state_saved)
  1192. return;
  1193. /* PCI Express register must be restored first */
  1194. pci_restore_pcie_state(dev);
  1195. pci_restore_pasid_state(dev);
  1196. pci_restore_pri_state(dev);
  1197. pci_restore_ats_state(dev);
  1198. pci_restore_vc_state(dev);
  1199. pci_restore_rebar_state(dev);
  1200. pci_restore_dpc_state(dev);
  1201. pci_cleanup_aer_error_status_regs(dev);
  1202. pci_restore_config_space(dev);
  1203. pci_restore_pcix_state(dev);
  1204. pci_restore_msi_state(dev);
  1205. /* Restore ACS and IOV configuration state */
  1206. pci_enable_acs(dev);
  1207. pci_restore_iov_state(dev);
  1208. dev->state_saved = false;
  1209. }
  1210. EXPORT_SYMBOL(pci_restore_state);
  1211. struct pci_saved_state {
  1212. u32 config_space[16];
  1213. struct pci_cap_saved_data cap[0];
  1214. };
  1215. /**
  1216. * pci_store_saved_state - Allocate and return an opaque struct containing
  1217. * the device saved state.
  1218. * @dev: PCI device that we're dealing with
  1219. *
  1220. * Return NULL if no state or error.
  1221. */
  1222. struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev)
  1223. {
  1224. struct pci_saved_state *state;
  1225. struct pci_cap_saved_state *tmp;
  1226. struct pci_cap_saved_data *cap;
  1227. size_t size;
  1228. if (!dev->state_saved)
  1229. return NULL;
  1230. size = sizeof(*state) + sizeof(struct pci_cap_saved_data);
  1231. hlist_for_each_entry(tmp, &dev->saved_cap_space, next)
  1232. size += sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1233. state = kzalloc(size, GFP_KERNEL);
  1234. if (!state)
  1235. return NULL;
  1236. memcpy(state->config_space, dev->saved_config_space,
  1237. sizeof(state->config_space));
  1238. cap = state->cap;
  1239. hlist_for_each_entry(tmp, &dev->saved_cap_space, next) {
  1240. size_t len = sizeof(struct pci_cap_saved_data) + tmp->cap.size;
  1241. memcpy(cap, &tmp->cap, len);
  1242. cap = (struct pci_cap_saved_data *)((u8 *)cap + len);
  1243. }
  1244. /* Empty cap_save terminates list */
  1245. return state;
  1246. }
  1247. EXPORT_SYMBOL_GPL(pci_store_saved_state);
  1248. /**
  1249. * pci_load_saved_state - Reload the provided save state into struct pci_dev.
  1250. * @dev: PCI device that we're dealing with
  1251. * @state: Saved state returned from pci_store_saved_state()
  1252. */
  1253. int pci_load_saved_state(struct pci_dev *dev,
  1254. struct pci_saved_state *state)
  1255. {
  1256. struct pci_cap_saved_data *cap;
  1257. dev->state_saved = false;
  1258. if (!state)
  1259. return 0;
  1260. memcpy(dev->saved_config_space, state->config_space,
  1261. sizeof(state->config_space));
  1262. cap = state->cap;
  1263. while (cap->size) {
  1264. struct pci_cap_saved_state *tmp;
  1265. tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
  1266. if (!tmp || tmp->cap.size != cap->size)
  1267. return -EINVAL;
  1268. memcpy(tmp->cap.data, cap->data, tmp->cap.size);
  1269. cap = (struct pci_cap_saved_data *)((u8 *)cap +
  1270. sizeof(struct pci_cap_saved_data) + cap->size);
  1271. }
  1272. dev->state_saved = true;
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL_GPL(pci_load_saved_state);
  1276. /**
  1277. * pci_load_and_free_saved_state - Reload the save state pointed to by state,
  1278. * and free the memory allocated for it.
  1279. * @dev: PCI device that we're dealing with
  1280. * @state: Pointer to saved state returned from pci_store_saved_state()
  1281. */
  1282. int pci_load_and_free_saved_state(struct pci_dev *dev,
  1283. struct pci_saved_state **state)
  1284. {
  1285. int ret = pci_load_saved_state(dev, *state);
  1286. kfree(*state);
  1287. *state = NULL;
  1288. return ret;
  1289. }
  1290. EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state);
  1291. int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
  1292. {
  1293. return pci_enable_resources(dev, bars);
  1294. }
  1295. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  1296. {
  1297. int err;
  1298. struct pci_dev *bridge;
  1299. u16 cmd;
  1300. u8 pin;
  1301. err = pci_set_power_state(dev, PCI_D0);
  1302. if (err < 0 && err != -EIO)
  1303. return err;
  1304. bridge = pci_upstream_bridge(dev);
  1305. if (bridge)
  1306. pcie_aspm_powersave_config_link(bridge);
  1307. err = pcibios_enable_device(dev, bars);
  1308. if (err < 0)
  1309. return err;
  1310. pci_fixup_device(pci_fixup_enable, dev);
  1311. if (dev->msi_enabled || dev->msix_enabled)
  1312. return 0;
  1313. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  1314. if (pin) {
  1315. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1316. if (cmd & PCI_COMMAND_INTX_DISABLE)
  1317. pci_write_config_word(dev, PCI_COMMAND,
  1318. cmd & ~PCI_COMMAND_INTX_DISABLE);
  1319. }
  1320. return 0;
  1321. }
  1322. /**
  1323. * pci_reenable_device - Resume abandoned device
  1324. * @dev: PCI device to be resumed
  1325. *
  1326. * Note this function is a backend of pci_default_resume and is not supposed
  1327. * to be called by normal code, write proper resume handler and use it instead.
  1328. */
  1329. int pci_reenable_device(struct pci_dev *dev)
  1330. {
  1331. if (pci_is_enabled(dev))
  1332. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  1333. return 0;
  1334. }
  1335. EXPORT_SYMBOL(pci_reenable_device);
  1336. static void pci_enable_bridge(struct pci_dev *dev)
  1337. {
  1338. struct pci_dev *bridge;
  1339. int retval;
  1340. bridge = pci_upstream_bridge(dev);
  1341. if (bridge)
  1342. pci_enable_bridge(bridge);
  1343. if (pci_is_enabled(dev)) {
  1344. if (!dev->is_busmaster)
  1345. pci_set_master(dev);
  1346. return;
  1347. }
  1348. retval = pci_enable_device(dev);
  1349. if (retval)
  1350. pci_err(dev, "Error enabling bridge (%d), continuing\n",
  1351. retval);
  1352. pci_set_master(dev);
  1353. }
  1354. static int pci_enable_device_flags(struct pci_dev *dev, unsigned long flags)
  1355. {
  1356. struct pci_dev *bridge;
  1357. int err;
  1358. int i, bars = 0;
  1359. /*
  1360. * Power state could be unknown at this point, either due to a fresh
  1361. * boot or a device removal call. So get the current power state
  1362. * so that things like MSI message writing will behave as expected
  1363. * (e.g. if the device really is in D0 at enable time).
  1364. */
  1365. if (dev->pm_cap) {
  1366. u16 pmcsr;
  1367. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1368. dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
  1369. }
  1370. if (atomic_inc_return(&dev->enable_cnt) > 1)
  1371. return 0; /* already enabled */
  1372. bridge = pci_upstream_bridge(dev);
  1373. if (bridge)
  1374. pci_enable_bridge(bridge);
  1375. /* only skip sriov related */
  1376. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  1377. if (dev->resource[i].flags & flags)
  1378. bars |= (1 << i);
  1379. for (i = PCI_BRIDGE_RESOURCES; i < DEVICE_COUNT_RESOURCE; i++)
  1380. if (dev->resource[i].flags & flags)
  1381. bars |= (1 << i);
  1382. err = do_pci_enable_device(dev, bars);
  1383. if (err < 0)
  1384. atomic_dec(&dev->enable_cnt);
  1385. return err;
  1386. }
  1387. /**
  1388. * pci_enable_device_io - Initialize a device for use with IO space
  1389. * @dev: PCI device to be initialized
  1390. *
  1391. * Initialize device before it's used by a driver. Ask low-level code
  1392. * to enable I/O resources. Wake up the device if it was suspended.
  1393. * Beware, this function can fail.
  1394. */
  1395. int pci_enable_device_io(struct pci_dev *dev)
  1396. {
  1397. return pci_enable_device_flags(dev, IORESOURCE_IO);
  1398. }
  1399. EXPORT_SYMBOL(pci_enable_device_io);
  1400. /**
  1401. * pci_enable_device_mem - Initialize a device for use with Memory space
  1402. * @dev: PCI device to be initialized
  1403. *
  1404. * Initialize device before it's used by a driver. Ask low-level code
  1405. * to enable Memory resources. Wake up the device if it was suspended.
  1406. * Beware, this function can fail.
  1407. */
  1408. int pci_enable_device_mem(struct pci_dev *dev)
  1409. {
  1410. return pci_enable_device_flags(dev, IORESOURCE_MEM);
  1411. }
  1412. EXPORT_SYMBOL(pci_enable_device_mem);
  1413. /**
  1414. * pci_enable_device - Initialize device before it's used by a driver.
  1415. * @dev: PCI device to be initialized
  1416. *
  1417. * Initialize device before it's used by a driver. Ask low-level code
  1418. * to enable I/O and memory. Wake up the device if it was suspended.
  1419. * Beware, this function can fail.
  1420. *
  1421. * Note we don't actually enable the device many times if we call
  1422. * this function repeatedly (we just increment the count).
  1423. */
  1424. int pci_enable_device(struct pci_dev *dev)
  1425. {
  1426. return pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  1427. }
  1428. EXPORT_SYMBOL(pci_enable_device);
  1429. /*
  1430. * Managed PCI resources. This manages device on/off, intx/msi/msix
  1431. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  1432. * there's no need to track it separately. pci_devres is initialized
  1433. * when a device is enabled using managed PCI device enable interface.
  1434. */
  1435. struct pci_devres {
  1436. unsigned int enabled:1;
  1437. unsigned int pinned:1;
  1438. unsigned int orig_intx:1;
  1439. unsigned int restore_intx:1;
  1440. unsigned int mwi:1;
  1441. u32 region_mask;
  1442. };
  1443. static void pcim_release(struct device *gendev, void *res)
  1444. {
  1445. struct pci_dev *dev = to_pci_dev(gendev);
  1446. struct pci_devres *this = res;
  1447. int i;
  1448. if (dev->msi_enabled)
  1449. pci_disable_msi(dev);
  1450. if (dev->msix_enabled)
  1451. pci_disable_msix(dev);
  1452. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  1453. if (this->region_mask & (1 << i))
  1454. pci_release_region(dev, i);
  1455. if (this->mwi)
  1456. pci_clear_mwi(dev);
  1457. if (this->restore_intx)
  1458. pci_intx(dev, this->orig_intx);
  1459. if (this->enabled && !this->pinned)
  1460. pci_disable_device(dev);
  1461. }
  1462. static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
  1463. {
  1464. struct pci_devres *dr, *new_dr;
  1465. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1466. if (dr)
  1467. return dr;
  1468. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  1469. if (!new_dr)
  1470. return NULL;
  1471. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  1472. }
  1473. static struct pci_devres *find_pci_dr(struct pci_dev *pdev)
  1474. {
  1475. if (pci_is_managed(pdev))
  1476. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  1477. return NULL;
  1478. }
  1479. /**
  1480. * pcim_enable_device - Managed pci_enable_device()
  1481. * @pdev: PCI device to be initialized
  1482. *
  1483. * Managed pci_enable_device().
  1484. */
  1485. int pcim_enable_device(struct pci_dev *pdev)
  1486. {
  1487. struct pci_devres *dr;
  1488. int rc;
  1489. dr = get_pci_dr(pdev);
  1490. if (unlikely(!dr))
  1491. return -ENOMEM;
  1492. if (dr->enabled)
  1493. return 0;
  1494. rc = pci_enable_device(pdev);
  1495. if (!rc) {
  1496. pdev->is_managed = 1;
  1497. dr->enabled = 1;
  1498. }
  1499. return rc;
  1500. }
  1501. EXPORT_SYMBOL(pcim_enable_device);
  1502. /**
  1503. * pcim_pin_device - Pin managed PCI device
  1504. * @pdev: PCI device to pin
  1505. *
  1506. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  1507. * driver detach. @pdev must have been enabled with
  1508. * pcim_enable_device().
  1509. */
  1510. void pcim_pin_device(struct pci_dev *pdev)
  1511. {
  1512. struct pci_devres *dr;
  1513. dr = find_pci_dr(pdev);
  1514. WARN_ON(!dr || !dr->enabled);
  1515. if (dr)
  1516. dr->pinned = 1;
  1517. }
  1518. EXPORT_SYMBOL(pcim_pin_device);
  1519. /*
  1520. * pcibios_add_device - provide arch specific hooks when adding device dev
  1521. * @dev: the PCI device being added
  1522. *
  1523. * Permits the platform to provide architecture specific functionality when
  1524. * devices are added. This is the default implementation. Architecture
  1525. * implementations can override this.
  1526. */
  1527. int __weak pcibios_add_device(struct pci_dev *dev)
  1528. {
  1529. return 0;
  1530. }
  1531. /**
  1532. * pcibios_release_device - provide arch specific hooks when releasing device dev
  1533. * @dev: the PCI device being released
  1534. *
  1535. * Permits the platform to provide architecture specific functionality when
  1536. * devices are released. This is the default implementation. Architecture
  1537. * implementations can override this.
  1538. */
  1539. void __weak pcibios_release_device(struct pci_dev *dev) {}
  1540. /**
  1541. * pcibios_disable_device - disable arch specific PCI resources for device dev
  1542. * @dev: the PCI device to disable
  1543. *
  1544. * Disables architecture specific PCI resources for the device. This
  1545. * is the default implementation. Architecture implementations can
  1546. * override this.
  1547. */
  1548. void __weak pcibios_disable_device(struct pci_dev *dev) {}
  1549. /**
  1550. * pcibios_penalize_isa_irq - penalize an ISA IRQ
  1551. * @irq: ISA IRQ to penalize
  1552. * @active: IRQ active or not
  1553. *
  1554. * Permits the platform to provide architecture-specific functionality when
  1555. * penalizing ISA IRQs. This is the default implementation. Architecture
  1556. * implementations can override this.
  1557. */
  1558. void __weak pcibios_penalize_isa_irq(int irq, int active) {}
  1559. static void do_pci_disable_device(struct pci_dev *dev)
  1560. {
  1561. u16 pci_command;
  1562. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  1563. if (pci_command & PCI_COMMAND_MASTER) {
  1564. pci_command &= ~PCI_COMMAND_MASTER;
  1565. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  1566. }
  1567. pcibios_disable_device(dev);
  1568. }
  1569. /**
  1570. * pci_disable_enabled_device - Disable device without updating enable_cnt
  1571. * @dev: PCI device to disable
  1572. *
  1573. * NOTE: This function is a backend of PCI power management routines and is
  1574. * not supposed to be called drivers.
  1575. */
  1576. void pci_disable_enabled_device(struct pci_dev *dev)
  1577. {
  1578. if (pci_is_enabled(dev))
  1579. do_pci_disable_device(dev);
  1580. }
  1581. /**
  1582. * pci_disable_device - Disable PCI device after use
  1583. * @dev: PCI device to be disabled
  1584. *
  1585. * Signal to the system that the PCI device is not in use by the system
  1586. * anymore. This only involves disabling PCI bus-mastering, if active.
  1587. *
  1588. * Note we don't actually disable the device until all callers of
  1589. * pci_enable_device() have called pci_disable_device().
  1590. */
  1591. void pci_disable_device(struct pci_dev *dev)
  1592. {
  1593. struct pci_devres *dr;
  1594. dr = find_pci_dr(dev);
  1595. if (dr)
  1596. dr->enabled = 0;
  1597. dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
  1598. "disabling already-disabled device");
  1599. if (atomic_dec_return(&dev->enable_cnt) != 0)
  1600. return;
  1601. do_pci_disable_device(dev);
  1602. dev->is_busmaster = 0;
  1603. }
  1604. EXPORT_SYMBOL(pci_disable_device);
  1605. /**
  1606. * pcibios_set_pcie_reset_state - set reset state for device dev
  1607. * @dev: the PCIe device reset
  1608. * @state: Reset state to enter into
  1609. *
  1610. *
  1611. * Sets the PCIe reset state for the device. This is the default
  1612. * implementation. Architecture implementations can override this.
  1613. */
  1614. int __weak pcibios_set_pcie_reset_state(struct pci_dev *dev,
  1615. enum pcie_reset_state state)
  1616. {
  1617. return -EINVAL;
  1618. }
  1619. /**
  1620. * pci_set_pcie_reset_state - set reset state for device dev
  1621. * @dev: the PCIe device reset
  1622. * @state: Reset state to enter into
  1623. *
  1624. *
  1625. * Sets the PCI reset state for the device.
  1626. */
  1627. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  1628. {
  1629. return pcibios_set_pcie_reset_state(dev, state);
  1630. }
  1631. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
  1632. /**
  1633. * pcie_clear_root_pme_status - Clear root port PME interrupt status.
  1634. * @dev: PCIe root port or event collector.
  1635. */
  1636. void pcie_clear_root_pme_status(struct pci_dev *dev)
  1637. {
  1638. pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
  1639. }
  1640. /**
  1641. * pci_check_pme_status - Check if given device has generated PME.
  1642. * @dev: Device to check.
  1643. *
  1644. * Check the PME status of the device and if set, clear it and clear PME enable
  1645. * (if set). Return 'true' if PME status and PME enable were both set or
  1646. * 'false' otherwise.
  1647. */
  1648. bool pci_check_pme_status(struct pci_dev *dev)
  1649. {
  1650. int pmcsr_pos;
  1651. u16 pmcsr;
  1652. bool ret = false;
  1653. if (!dev->pm_cap)
  1654. return false;
  1655. pmcsr_pos = dev->pm_cap + PCI_PM_CTRL;
  1656. pci_read_config_word(dev, pmcsr_pos, &pmcsr);
  1657. if (!(pmcsr & PCI_PM_CTRL_PME_STATUS))
  1658. return false;
  1659. /* Clear PME status. */
  1660. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1661. if (pmcsr & PCI_PM_CTRL_PME_ENABLE) {
  1662. /* Disable PME to avoid interrupt flood. */
  1663. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1664. ret = true;
  1665. }
  1666. pci_write_config_word(dev, pmcsr_pos, pmcsr);
  1667. return ret;
  1668. }
  1669. /**
  1670. * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
  1671. * @dev: Device to handle.
  1672. * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
  1673. *
  1674. * Check if @dev has generated PME and queue a resume request for it in that
  1675. * case.
  1676. */
  1677. static int pci_pme_wakeup(struct pci_dev *dev, void *pme_poll_reset)
  1678. {
  1679. if (pme_poll_reset && dev->pme_poll)
  1680. dev->pme_poll = false;
  1681. if (pci_check_pme_status(dev)) {
  1682. pci_wakeup_event(dev);
  1683. pm_request_resume(&dev->dev);
  1684. }
  1685. return 0;
  1686. }
  1687. /**
  1688. * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
  1689. * @bus: Top bus of the subtree to walk.
  1690. */
  1691. void pci_pme_wakeup_bus(struct pci_bus *bus)
  1692. {
  1693. if (bus)
  1694. pci_walk_bus(bus, pci_pme_wakeup, (void *)true);
  1695. }
  1696. /**
  1697. * pci_pme_capable - check the capability of PCI device to generate PME#
  1698. * @dev: PCI device to handle.
  1699. * @state: PCI state from which device will issue PME#.
  1700. */
  1701. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  1702. {
  1703. if (!dev->pm_cap)
  1704. return false;
  1705. return !!(dev->pme_support & (1 << state));
  1706. }
  1707. EXPORT_SYMBOL(pci_pme_capable);
  1708. static void pci_pme_list_scan(struct work_struct *work)
  1709. {
  1710. struct pci_pme_device *pme_dev, *n;
  1711. mutex_lock(&pci_pme_list_mutex);
  1712. list_for_each_entry_safe(pme_dev, n, &pci_pme_list, list) {
  1713. if (pme_dev->dev->pme_poll) {
  1714. struct pci_dev *bridge;
  1715. bridge = pme_dev->dev->bus->self;
  1716. /*
  1717. * If bridge is in low power state, the
  1718. * configuration space of subordinate devices
  1719. * may be not accessible
  1720. */
  1721. if (bridge && bridge->current_state != PCI_D0)
  1722. continue;
  1723. pci_pme_wakeup(pme_dev->dev, NULL);
  1724. } else {
  1725. list_del(&pme_dev->list);
  1726. kfree(pme_dev);
  1727. }
  1728. }
  1729. if (!list_empty(&pci_pme_list))
  1730. queue_delayed_work(system_freezable_wq, &pci_pme_work,
  1731. msecs_to_jiffies(PME_TIMEOUT));
  1732. mutex_unlock(&pci_pme_list_mutex);
  1733. }
  1734. static void __pci_pme_active(struct pci_dev *dev, bool enable)
  1735. {
  1736. u16 pmcsr;
  1737. if (!dev->pme_support)
  1738. return;
  1739. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1740. /* Clear PME_Status by writing 1 to it and enable PME# */
  1741. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  1742. if (!enable)
  1743. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1744. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1745. }
  1746. /**
  1747. * pci_pme_restore - Restore PME configuration after config space restore.
  1748. * @dev: PCI device to update.
  1749. */
  1750. void pci_pme_restore(struct pci_dev *dev)
  1751. {
  1752. u16 pmcsr;
  1753. if (!dev->pme_support)
  1754. return;
  1755. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  1756. if (dev->wakeup_prepared) {
  1757. pmcsr |= PCI_PM_CTRL_PME_ENABLE;
  1758. pmcsr &= ~PCI_PM_CTRL_PME_STATUS;
  1759. } else {
  1760. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  1761. pmcsr |= PCI_PM_CTRL_PME_STATUS;
  1762. }
  1763. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  1764. }
  1765. /**
  1766. * pci_pme_active - enable or disable PCI device's PME# function
  1767. * @dev: PCI device to handle.
  1768. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  1769. *
  1770. * The caller must verify that the device is capable of generating PME# before
  1771. * calling this function with @enable equal to 'true'.
  1772. */
  1773. void pci_pme_active(struct pci_dev *dev, bool enable)
  1774. {
  1775. __pci_pme_active(dev, enable);
  1776. /*
  1777. * PCI (as opposed to PCIe) PME requires that the device have
  1778. * its PME# line hooked up correctly. Not all hardware vendors
  1779. * do this, so the PME never gets delivered and the device
  1780. * remains asleep. The easiest way around this is to
  1781. * periodically walk the list of suspended devices and check
  1782. * whether any have their PME flag set. The assumption is that
  1783. * we'll wake up often enough anyway that this won't be a huge
  1784. * hit, and the power savings from the devices will still be a
  1785. * win.
  1786. *
  1787. * Although PCIe uses in-band PME message instead of PME# line
  1788. * to report PME, PME does not work for some PCIe devices in
  1789. * reality. For example, there are devices that set their PME
  1790. * status bits, but don't really bother to send a PME message;
  1791. * there are PCI Express Root Ports that don't bother to
  1792. * trigger interrupts when they receive PME messages from the
  1793. * devices below. So PME poll is used for PCIe devices too.
  1794. */
  1795. if (dev->pme_poll) {
  1796. struct pci_pme_device *pme_dev;
  1797. if (enable) {
  1798. pme_dev = kmalloc(sizeof(struct pci_pme_device),
  1799. GFP_KERNEL);
  1800. if (!pme_dev) {
  1801. pci_warn(dev, "can't enable PME#\n");
  1802. return;
  1803. }
  1804. pme_dev->dev = dev;
  1805. mutex_lock(&pci_pme_list_mutex);
  1806. list_add(&pme_dev->list, &pci_pme_list);
  1807. if (list_is_singular(&pci_pme_list))
  1808. queue_delayed_work(system_freezable_wq,
  1809. &pci_pme_work,
  1810. msecs_to_jiffies(PME_TIMEOUT));
  1811. mutex_unlock(&pci_pme_list_mutex);
  1812. } else {
  1813. mutex_lock(&pci_pme_list_mutex);
  1814. list_for_each_entry(pme_dev, &pci_pme_list, list) {
  1815. if (pme_dev->dev == dev) {
  1816. list_del(&pme_dev->list);
  1817. kfree(pme_dev);
  1818. break;
  1819. }
  1820. }
  1821. mutex_unlock(&pci_pme_list_mutex);
  1822. }
  1823. }
  1824. pci_dbg(dev, "PME# %s\n", enable ? "enabled" : "disabled");
  1825. }
  1826. EXPORT_SYMBOL(pci_pme_active);
  1827. /**
  1828. * __pci_enable_wake - enable PCI device as wakeup event source
  1829. * @dev: PCI device affected
  1830. * @state: PCI state from which device will issue wakeup events
  1831. * @enable: True to enable event generation; false to disable
  1832. *
  1833. * This enables the device as a wakeup event source, or disables it.
  1834. * When such events involves platform-specific hooks, those hooks are
  1835. * called automatically by this routine.
  1836. *
  1837. * Devices with legacy power management (no standard PCI PM capabilities)
  1838. * always require such platform hooks.
  1839. *
  1840. * RETURN VALUE:
  1841. * 0 is returned on success
  1842. * -EINVAL is returned if device is not supposed to wake up the system
  1843. * Error code depending on the platform is returned if both the platform and
  1844. * the native mechanism fail to enable the generation of wake-up events
  1845. */
  1846. static int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable)
  1847. {
  1848. int ret = 0;
  1849. /*
  1850. * Bridges that are not power-manageable directly only signal
  1851. * wakeup on behalf of subordinate devices which is set up
  1852. * elsewhere, so skip them. However, bridges that are
  1853. * power-manageable may signal wakeup for themselves (for example,
  1854. * on a hotplug event) and they need to be covered here.
  1855. */
  1856. if (!pci_power_manageable(dev))
  1857. return 0;
  1858. /* Don't do the same thing twice in a row for one device. */
  1859. if (!!enable == !!dev->wakeup_prepared)
  1860. return 0;
  1861. /*
  1862. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  1863. * Anderson we should be doing PME# wake enable followed by ACPI wake
  1864. * enable. To disable wake-up we call the platform first, for symmetry.
  1865. */
  1866. if (enable) {
  1867. int error;
  1868. if (pci_pme_capable(dev, state))
  1869. pci_pme_active(dev, true);
  1870. else
  1871. ret = 1;
  1872. error = platform_pci_set_wakeup(dev, true);
  1873. if (ret)
  1874. ret = error;
  1875. if (!ret)
  1876. dev->wakeup_prepared = true;
  1877. } else {
  1878. platform_pci_set_wakeup(dev, false);
  1879. pci_pme_active(dev, false);
  1880. dev->wakeup_prepared = false;
  1881. }
  1882. return ret;
  1883. }
  1884. /**
  1885. * pci_enable_wake - change wakeup settings for a PCI device
  1886. * @pci_dev: Target device
  1887. * @state: PCI state from which device will issue wakeup events
  1888. * @enable: Whether or not to enable event generation
  1889. *
  1890. * If @enable is set, check device_may_wakeup() for the device before calling
  1891. * __pci_enable_wake() for it.
  1892. */
  1893. int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
  1894. {
  1895. if (enable && !device_may_wakeup(&pci_dev->dev))
  1896. return -EINVAL;
  1897. return __pci_enable_wake(pci_dev, state, enable);
  1898. }
  1899. EXPORT_SYMBOL(pci_enable_wake);
  1900. /**
  1901. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  1902. * @dev: PCI device to prepare
  1903. * @enable: True to enable wake-up event generation; false to disable
  1904. *
  1905. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  1906. * and this function allows them to set that up cleanly - pci_enable_wake()
  1907. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  1908. * ordering constraints.
  1909. *
  1910. * This function only returns error code if the device is not allowed to wake
  1911. * up the system from sleep or it is not capable of generating PME# from both
  1912. * D3_hot and D3_cold and the platform is unable to enable wake-up power for it.
  1913. */
  1914. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  1915. {
  1916. return pci_pme_capable(dev, PCI_D3cold) ?
  1917. pci_enable_wake(dev, PCI_D3cold, enable) :
  1918. pci_enable_wake(dev, PCI_D3hot, enable);
  1919. }
  1920. EXPORT_SYMBOL(pci_wake_from_d3);
  1921. /**
  1922. * pci_target_state - find an appropriate low power state for a given PCI dev
  1923. * @dev: PCI device
  1924. * @wakeup: Whether or not wakeup functionality will be enabled for the device.
  1925. *
  1926. * Use underlying platform code to find a supported low power state for @dev.
  1927. * If the platform can't manage @dev, return the deepest state from which it
  1928. * can generate wake events, based on any available PME info.
  1929. */
  1930. static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup)
  1931. {
  1932. pci_power_t target_state = PCI_D3hot;
  1933. if (platform_pci_power_manageable(dev)) {
  1934. /*
  1935. * Call the platform to find the target state for the device.
  1936. */
  1937. pci_power_t state = platform_pci_choose_state(dev);
  1938. switch (state) {
  1939. case PCI_POWER_ERROR:
  1940. case PCI_UNKNOWN:
  1941. break;
  1942. case PCI_D1:
  1943. case PCI_D2:
  1944. if (pci_no_d1d2(dev))
  1945. break;
  1946. /* else: fall through */
  1947. default:
  1948. target_state = state;
  1949. }
  1950. return target_state;
  1951. }
  1952. if (!dev->pm_cap)
  1953. target_state = PCI_D0;
  1954. /*
  1955. * If the device is in D3cold even though it's not power-manageable by
  1956. * the platform, it may have been powered down by non-standard means.
  1957. * Best to let it slumber.
  1958. */
  1959. if (dev->current_state == PCI_D3cold)
  1960. target_state = PCI_D3cold;
  1961. if (wakeup) {
  1962. /*
  1963. * Find the deepest state from which the device can generate
  1964. * PME#.
  1965. */
  1966. if (dev->pme_support) {
  1967. while (target_state
  1968. && !(dev->pme_support & (1 << target_state)))
  1969. target_state--;
  1970. }
  1971. }
  1972. return target_state;
  1973. }
  1974. /**
  1975. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1976. * @dev: Device to handle.
  1977. *
  1978. * Choose the power state appropriate for the device depending on whether
  1979. * it can wake up the system and/or is power manageable by the platform
  1980. * (PCI_D3hot is the default) and put the device into that state.
  1981. */
  1982. int pci_prepare_to_sleep(struct pci_dev *dev)
  1983. {
  1984. bool wakeup = device_may_wakeup(&dev->dev);
  1985. pci_power_t target_state = pci_target_state(dev, wakeup);
  1986. int error;
  1987. if (target_state == PCI_POWER_ERROR)
  1988. return -EIO;
  1989. pci_enable_wake(dev, target_state, wakeup);
  1990. error = pci_set_power_state(dev, target_state);
  1991. if (error)
  1992. pci_enable_wake(dev, target_state, false);
  1993. return error;
  1994. }
  1995. EXPORT_SYMBOL(pci_prepare_to_sleep);
  1996. /**
  1997. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1998. * @dev: Device to handle.
  1999. *
  2000. * Disable device's system wake-up capability and put it into D0.
  2001. */
  2002. int pci_back_from_sleep(struct pci_dev *dev)
  2003. {
  2004. pci_enable_wake(dev, PCI_D0, false);
  2005. return pci_set_power_state(dev, PCI_D0);
  2006. }
  2007. EXPORT_SYMBOL(pci_back_from_sleep);
  2008. /**
  2009. * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
  2010. * @dev: PCI device being suspended.
  2011. *
  2012. * Prepare @dev to generate wake-up events at run time and put it into a low
  2013. * power state.
  2014. */
  2015. int pci_finish_runtime_suspend(struct pci_dev *dev)
  2016. {
  2017. pci_power_t target_state;
  2018. int error;
  2019. target_state = pci_target_state(dev, device_can_wakeup(&dev->dev));
  2020. if (target_state == PCI_POWER_ERROR)
  2021. return -EIO;
  2022. dev->runtime_d3cold = target_state == PCI_D3cold;
  2023. __pci_enable_wake(dev, target_state, pci_dev_run_wake(dev));
  2024. error = pci_set_power_state(dev, target_state);
  2025. if (error) {
  2026. pci_enable_wake(dev, target_state, false);
  2027. dev->runtime_d3cold = false;
  2028. }
  2029. return error;
  2030. }
  2031. /**
  2032. * pci_dev_run_wake - Check if device can generate run-time wake-up events.
  2033. * @dev: Device to check.
  2034. *
  2035. * Return true if the device itself is capable of generating wake-up events
  2036. * (through the platform or using the native PCIe PME) or if the device supports
  2037. * PME and one of its upstream bridges can generate wake-up events.
  2038. */
  2039. bool pci_dev_run_wake(struct pci_dev *dev)
  2040. {
  2041. struct pci_bus *bus = dev->bus;
  2042. if (!dev->pme_support)
  2043. return false;
  2044. /* PME-capable in principle, but not from the target power state */
  2045. if (!pci_pme_capable(dev, pci_target_state(dev, true)))
  2046. return false;
  2047. if (device_can_wakeup(&dev->dev))
  2048. return true;
  2049. while (bus->parent) {
  2050. struct pci_dev *bridge = bus->self;
  2051. if (device_can_wakeup(&bridge->dev))
  2052. return true;
  2053. bus = bus->parent;
  2054. }
  2055. /* We have reached the root bus. */
  2056. if (bus->bridge)
  2057. return device_can_wakeup(bus->bridge);
  2058. return false;
  2059. }
  2060. EXPORT_SYMBOL_GPL(pci_dev_run_wake);
  2061. /**
  2062. * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
  2063. * @pci_dev: Device to check.
  2064. *
  2065. * Return 'true' if the device is runtime-suspended, it doesn't have to be
  2066. * reconfigured due to wakeup settings difference between system and runtime
  2067. * suspend and the current power state of it is suitable for the upcoming
  2068. * (system) transition.
  2069. *
  2070. * If the device is not configured for system wakeup, disable PME for it before
  2071. * returning 'true' to prevent it from waking up the system unnecessarily.
  2072. */
  2073. bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
  2074. {
  2075. struct device *dev = &pci_dev->dev;
  2076. bool wakeup = device_may_wakeup(dev);
  2077. if (!pm_runtime_suspended(dev)
  2078. || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
  2079. || platform_pci_need_resume(pci_dev))
  2080. return false;
  2081. /*
  2082. * At this point the device is good to go unless it's been configured
  2083. * to generate PME at the runtime suspend time, but it is not supposed
  2084. * to wake up the system. In that case, simply disable PME for it
  2085. * (it will have to be re-enabled on exit from system resume).
  2086. *
  2087. * If the device's power state is D3cold and the platform check above
  2088. * hasn't triggered, the device's configuration is suitable and we don't
  2089. * need to manipulate it at all.
  2090. */
  2091. spin_lock_irq(&dev->power.lock);
  2092. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
  2093. !wakeup)
  2094. __pci_pme_active(pci_dev, false);
  2095. spin_unlock_irq(&dev->power.lock);
  2096. return true;
  2097. }
  2098. /**
  2099. * pci_dev_complete_resume - Finalize resume from system sleep for a device.
  2100. * @pci_dev: Device to handle.
  2101. *
  2102. * If the device is runtime suspended and wakeup-capable, enable PME for it as
  2103. * it might have been disabled during the prepare phase of system suspend if
  2104. * the device was not configured for system wakeup.
  2105. */
  2106. void pci_dev_complete_resume(struct pci_dev *pci_dev)
  2107. {
  2108. struct device *dev = &pci_dev->dev;
  2109. if (!pci_dev_run_wake(pci_dev))
  2110. return;
  2111. spin_lock_irq(&dev->power.lock);
  2112. if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold)
  2113. __pci_pme_active(pci_dev, true);
  2114. spin_unlock_irq(&dev->power.lock);
  2115. }
  2116. void pci_config_pm_runtime_get(struct pci_dev *pdev)
  2117. {
  2118. struct device *dev = &pdev->dev;
  2119. struct device *parent = dev->parent;
  2120. if (parent)
  2121. pm_runtime_get_sync(parent);
  2122. pm_runtime_get_noresume(dev);
  2123. /*
  2124. * pdev->current_state is set to PCI_D3cold during suspending,
  2125. * so wait until suspending completes
  2126. */
  2127. pm_runtime_barrier(dev);
  2128. /*
  2129. * Only need to resume devices in D3cold, because config
  2130. * registers are still accessible for devices suspended but
  2131. * not in D3cold.
  2132. */
  2133. if (pdev->current_state == PCI_D3cold)
  2134. pm_runtime_resume(dev);
  2135. }
  2136. void pci_config_pm_runtime_put(struct pci_dev *pdev)
  2137. {
  2138. struct device *dev = &pdev->dev;
  2139. struct device *parent = dev->parent;
  2140. pm_runtime_put(dev);
  2141. if (parent)
  2142. pm_runtime_put_sync(parent);
  2143. }
  2144. /**
  2145. * pci_bridge_d3_possible - Is it possible to put the bridge into D3
  2146. * @bridge: Bridge to check
  2147. *
  2148. * This function checks if it is possible to move the bridge to D3.
  2149. * Currently we only allow D3 for recent enough PCIe ports and Thunderbolt.
  2150. */
  2151. bool pci_bridge_d3_possible(struct pci_dev *bridge)
  2152. {
  2153. if (!pci_is_pcie(bridge))
  2154. return false;
  2155. switch (pci_pcie_type(bridge)) {
  2156. case PCI_EXP_TYPE_ROOT_PORT:
  2157. case PCI_EXP_TYPE_UPSTREAM:
  2158. case PCI_EXP_TYPE_DOWNSTREAM:
  2159. if (pci_bridge_d3_disable)
  2160. return false;
  2161. /*
  2162. * Hotplug ports handled by firmware in System Management Mode
  2163. * may not be put into D3 by the OS (Thunderbolt on non-Macs).
  2164. */
  2165. if (bridge->is_hotplug_bridge && !pciehp_is_native(bridge))
  2166. return false;
  2167. if (pci_bridge_d3_force)
  2168. return true;
  2169. /* Even the oldest 2010 Thunderbolt controller supports D3. */
  2170. if (bridge->is_thunderbolt)
  2171. return true;
  2172. /* Platform might know better if the bridge supports D3 */
  2173. if (platform_pci_bridge_d3(bridge))
  2174. return true;
  2175. /*
  2176. * Hotplug ports handled natively by the OS were not validated
  2177. * by vendors for runtime D3 at least until 2018 because there
  2178. * was no OS support.
  2179. */
  2180. if (bridge->is_hotplug_bridge)
  2181. return false;
  2182. /*
  2183. * It should be safe to put PCIe ports from 2015 or newer
  2184. * to D3.
  2185. */
  2186. if (dmi_get_bios_year() >= 2015)
  2187. return true;
  2188. break;
  2189. }
  2190. return false;
  2191. }
  2192. static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
  2193. {
  2194. bool *d3cold_ok = data;
  2195. if (/* The device needs to be allowed to go D3cold ... */
  2196. dev->no_d3cold || !dev->d3cold_allowed ||
  2197. /* ... and if it is wakeup capable to do so from D3cold. */
  2198. (device_may_wakeup(&dev->dev) &&
  2199. !pci_pme_capable(dev, PCI_D3cold)) ||
  2200. /* If it is a bridge it must be allowed to go to D3. */
  2201. !pci_power_manageable(dev))
  2202. *d3cold_ok = false;
  2203. return !*d3cold_ok;
  2204. }
  2205. /*
  2206. * pci_bridge_d3_update - Update bridge D3 capabilities
  2207. * @dev: PCI device which is changed
  2208. *
  2209. * Update upstream bridge PM capabilities accordingly depending on if the
  2210. * device PM configuration was changed or the device is being removed. The
  2211. * change is also propagated upstream.
  2212. */
  2213. void pci_bridge_d3_update(struct pci_dev *dev)
  2214. {
  2215. bool remove = !device_is_registered(&dev->dev);
  2216. struct pci_dev *bridge;
  2217. bool d3cold_ok = true;
  2218. bridge = pci_upstream_bridge(dev);
  2219. if (!bridge || !pci_bridge_d3_possible(bridge))
  2220. return;
  2221. /*
  2222. * If D3 is currently allowed for the bridge, removing one of its
  2223. * children won't change that.
  2224. */
  2225. if (remove && bridge->bridge_d3)
  2226. return;
  2227. /*
  2228. * If D3 is currently allowed for the bridge and a child is added or
  2229. * changed, disallowance of D3 can only be caused by that child, so
  2230. * we only need to check that single device, not any of its siblings.
  2231. *
  2232. * If D3 is currently not allowed for the bridge, checking the device
  2233. * first may allow us to skip checking its siblings.
  2234. */
  2235. if (!remove)
  2236. pci_dev_check_d3cold(dev, &d3cold_ok);
  2237. /*
  2238. * If D3 is currently not allowed for the bridge, this may be caused
  2239. * either by the device being changed/removed or any of its siblings,
  2240. * so we need to go through all children to find out if one of them
  2241. * continues to block D3.
  2242. */
  2243. if (d3cold_ok && !bridge->bridge_d3)
  2244. pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
  2245. &d3cold_ok);
  2246. if (bridge->bridge_d3 != d3cold_ok) {
  2247. bridge->bridge_d3 = d3cold_ok;
  2248. /* Propagate change to upstream bridges */
  2249. pci_bridge_d3_update(bridge);
  2250. }
  2251. }
  2252. /**
  2253. * pci_d3cold_enable - Enable D3cold for device
  2254. * @dev: PCI device to handle
  2255. *
  2256. * This function can be used in drivers to enable D3cold from the device
  2257. * they handle. It also updates upstream PCI bridge PM capabilities
  2258. * accordingly.
  2259. */
  2260. void pci_d3cold_enable(struct pci_dev *dev)
  2261. {
  2262. if (dev->no_d3cold) {
  2263. dev->no_d3cold = false;
  2264. pci_bridge_d3_update(dev);
  2265. }
  2266. }
  2267. EXPORT_SYMBOL_GPL(pci_d3cold_enable);
  2268. /**
  2269. * pci_d3cold_disable - Disable D3cold for device
  2270. * @dev: PCI device to handle
  2271. *
  2272. * This function can be used in drivers to disable D3cold from the device
  2273. * they handle. It also updates upstream PCI bridge PM capabilities
  2274. * accordingly.
  2275. */
  2276. void pci_d3cold_disable(struct pci_dev *dev)
  2277. {
  2278. if (!dev->no_d3cold) {
  2279. dev->no_d3cold = true;
  2280. pci_bridge_d3_update(dev);
  2281. }
  2282. }
  2283. EXPORT_SYMBOL_GPL(pci_d3cold_disable);
  2284. /**
  2285. * pci_pm_init - Initialize PM functions of given PCI device
  2286. * @dev: PCI device to handle.
  2287. */
  2288. void pci_pm_init(struct pci_dev *dev)
  2289. {
  2290. int pm;
  2291. u16 status;
  2292. u16 pmc;
  2293. pm_runtime_forbid(&dev->dev);
  2294. pm_runtime_set_active(&dev->dev);
  2295. pm_runtime_enable(&dev->dev);
  2296. device_enable_async_suspend(&dev->dev);
  2297. dev->wakeup_prepared = false;
  2298. dev->pm_cap = 0;
  2299. dev->pme_support = 0;
  2300. /* find PCI PM capability in list */
  2301. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  2302. if (!pm)
  2303. return;
  2304. /* Check device's ability to generate PME# */
  2305. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  2306. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  2307. pci_err(dev, "unsupported PM cap regs version (%u)\n",
  2308. pmc & PCI_PM_CAP_VER_MASK);
  2309. return;
  2310. }
  2311. dev->pm_cap = pm;
  2312. dev->d3_delay = PCI_PM_D3_WAIT;
  2313. dev->d3cold_delay = PCI_PM_D3COLD_WAIT;
  2314. dev->bridge_d3 = pci_bridge_d3_possible(dev);
  2315. dev->d3cold_allowed = true;
  2316. dev->d1_support = false;
  2317. dev->d2_support = false;
  2318. if (!pci_no_d1d2(dev)) {
  2319. if (pmc & PCI_PM_CAP_D1)
  2320. dev->d1_support = true;
  2321. if (pmc & PCI_PM_CAP_D2)
  2322. dev->d2_support = true;
  2323. if (dev->d1_support || dev->d2_support)
  2324. pci_printk(KERN_DEBUG, dev, "supports%s%s\n",
  2325. dev->d1_support ? " D1" : "",
  2326. dev->d2_support ? " D2" : "");
  2327. }
  2328. pmc &= PCI_PM_CAP_PME_MASK;
  2329. if (pmc) {
  2330. pci_printk(KERN_DEBUG, dev, "PME# supported from%s%s%s%s%s\n",
  2331. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  2332. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  2333. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  2334. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  2335. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  2336. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  2337. dev->pme_poll = true;
  2338. /*
  2339. * Make device's PM flags reflect the wake-up capability, but
  2340. * let the user space enable it to wake up the system as needed.
  2341. */
  2342. device_set_wakeup_capable(&dev->dev, true);
  2343. /* Disable the PME# generation functionality */
  2344. pci_pme_active(dev, false);
  2345. }
  2346. pci_read_config_word(dev, PCI_STATUS, &status);
  2347. if (status & PCI_STATUS_IMM_READY)
  2348. dev->imm_ready = 1;
  2349. }
  2350. static unsigned long pci_ea_flags(struct pci_dev *dev, u8 prop)
  2351. {
  2352. unsigned long flags = IORESOURCE_PCI_FIXED | IORESOURCE_PCI_EA_BEI;
  2353. switch (prop) {
  2354. case PCI_EA_P_MEM:
  2355. case PCI_EA_P_VF_MEM:
  2356. flags |= IORESOURCE_MEM;
  2357. break;
  2358. case PCI_EA_P_MEM_PREFETCH:
  2359. case PCI_EA_P_VF_MEM_PREFETCH:
  2360. flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  2361. break;
  2362. case PCI_EA_P_IO:
  2363. flags |= IORESOURCE_IO;
  2364. break;
  2365. default:
  2366. return 0;
  2367. }
  2368. return flags;
  2369. }
  2370. static struct resource *pci_ea_get_resource(struct pci_dev *dev, u8 bei,
  2371. u8 prop)
  2372. {
  2373. if (bei <= PCI_EA_BEI_BAR5 && prop <= PCI_EA_P_IO)
  2374. return &dev->resource[bei];
  2375. #ifdef CONFIG_PCI_IOV
  2376. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5 &&
  2377. (prop == PCI_EA_P_VF_MEM || prop == PCI_EA_P_VF_MEM_PREFETCH))
  2378. return &dev->resource[PCI_IOV_RESOURCES +
  2379. bei - PCI_EA_BEI_VF_BAR0];
  2380. #endif
  2381. else if (bei == PCI_EA_BEI_ROM)
  2382. return &dev->resource[PCI_ROM_RESOURCE];
  2383. else
  2384. return NULL;
  2385. }
  2386. /* Read an Enhanced Allocation (EA) entry */
  2387. static int pci_ea_read(struct pci_dev *dev, int offset)
  2388. {
  2389. struct resource *res;
  2390. int ent_size, ent_offset = offset;
  2391. resource_size_t start, end;
  2392. unsigned long flags;
  2393. u32 dw0, bei, base, max_offset;
  2394. u8 prop;
  2395. bool support_64 = (sizeof(resource_size_t) >= 8);
  2396. pci_read_config_dword(dev, ent_offset, &dw0);
  2397. ent_offset += 4;
  2398. /* Entry size field indicates DWORDs after 1st */
  2399. ent_size = ((dw0 & PCI_EA_ES) + 1) << 2;
  2400. if (!(dw0 & PCI_EA_ENABLE)) /* Entry not enabled */
  2401. goto out;
  2402. bei = (dw0 & PCI_EA_BEI) >> 4;
  2403. prop = (dw0 & PCI_EA_PP) >> 8;
  2404. /*
  2405. * If the Property is in the reserved range, try the Secondary
  2406. * Property instead.
  2407. */
  2408. if (prop > PCI_EA_P_BRIDGE_IO && prop < PCI_EA_P_MEM_RESERVED)
  2409. prop = (dw0 & PCI_EA_SP) >> 16;
  2410. if (prop > PCI_EA_P_BRIDGE_IO)
  2411. goto out;
  2412. res = pci_ea_get_resource(dev, bei, prop);
  2413. if (!res) {
  2414. pci_err(dev, "Unsupported EA entry BEI: %u\n", bei);
  2415. goto out;
  2416. }
  2417. flags = pci_ea_flags(dev, prop);
  2418. if (!flags) {
  2419. pci_err(dev, "Unsupported EA properties: %#x\n", prop);
  2420. goto out;
  2421. }
  2422. /* Read Base */
  2423. pci_read_config_dword(dev, ent_offset, &base);
  2424. start = (base & PCI_EA_FIELD_MASK);
  2425. ent_offset += 4;
  2426. /* Read MaxOffset */
  2427. pci_read_config_dword(dev, ent_offset, &max_offset);
  2428. ent_offset += 4;
  2429. /* Read Base MSBs (if 64-bit entry) */
  2430. if (base & PCI_EA_IS_64) {
  2431. u32 base_upper;
  2432. pci_read_config_dword(dev, ent_offset, &base_upper);
  2433. ent_offset += 4;
  2434. flags |= IORESOURCE_MEM_64;
  2435. /* entry starts above 32-bit boundary, can't use */
  2436. if (!support_64 && base_upper)
  2437. goto out;
  2438. if (support_64)
  2439. start |= ((u64)base_upper << 32);
  2440. }
  2441. end = start + (max_offset | 0x03);
  2442. /* Read MaxOffset MSBs (if 64-bit entry) */
  2443. if (max_offset & PCI_EA_IS_64) {
  2444. u32 max_offset_upper;
  2445. pci_read_config_dword(dev, ent_offset, &max_offset_upper);
  2446. ent_offset += 4;
  2447. flags |= IORESOURCE_MEM_64;
  2448. /* entry too big, can't use */
  2449. if (!support_64 && max_offset_upper)
  2450. goto out;
  2451. if (support_64)
  2452. end += ((u64)max_offset_upper << 32);
  2453. }
  2454. if (end < start) {
  2455. pci_err(dev, "EA Entry crosses address boundary\n");
  2456. goto out;
  2457. }
  2458. if (ent_size != ent_offset - offset) {
  2459. pci_err(dev, "EA Entry Size (%d) does not match length read (%d)\n",
  2460. ent_size, ent_offset - offset);
  2461. goto out;
  2462. }
  2463. res->name = pci_name(dev);
  2464. res->start = start;
  2465. res->end = end;
  2466. res->flags = flags;
  2467. if (bei <= PCI_EA_BEI_BAR5)
  2468. pci_printk(KERN_DEBUG, dev, "BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2469. bei, res, prop);
  2470. else if (bei == PCI_EA_BEI_ROM)
  2471. pci_printk(KERN_DEBUG, dev, "ROM: %pR (from Enhanced Allocation, properties %#02x)\n",
  2472. res, prop);
  2473. else if (bei >= PCI_EA_BEI_VF_BAR0 && bei <= PCI_EA_BEI_VF_BAR5)
  2474. pci_printk(KERN_DEBUG, dev, "VF BAR %d: %pR (from Enhanced Allocation, properties %#02x)\n",
  2475. bei - PCI_EA_BEI_VF_BAR0, res, prop);
  2476. else
  2477. pci_printk(KERN_DEBUG, dev, "BEI %d res: %pR (from Enhanced Allocation, properties %#02x)\n",
  2478. bei, res, prop);
  2479. out:
  2480. return offset + ent_size;
  2481. }
  2482. /* Enhanced Allocation Initialization */
  2483. void pci_ea_init(struct pci_dev *dev)
  2484. {
  2485. int ea;
  2486. u8 num_ent;
  2487. int offset;
  2488. int i;
  2489. /* find PCI EA capability in list */
  2490. ea = pci_find_capability(dev, PCI_CAP_ID_EA);
  2491. if (!ea)
  2492. return;
  2493. /* determine the number of entries */
  2494. pci_bus_read_config_byte(dev->bus, dev->devfn, ea + PCI_EA_NUM_ENT,
  2495. &num_ent);
  2496. num_ent &= PCI_EA_NUM_ENT_MASK;
  2497. offset = ea + PCI_EA_FIRST_ENT;
  2498. /* Skip DWORD 2 for type 1 functions */
  2499. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  2500. offset += 4;
  2501. /* parse each EA entry */
  2502. for (i = 0; i < num_ent; ++i)
  2503. offset = pci_ea_read(dev, offset);
  2504. }
  2505. static void pci_add_saved_cap(struct pci_dev *pci_dev,
  2506. struct pci_cap_saved_state *new_cap)
  2507. {
  2508. hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
  2509. }
  2510. /**
  2511. * _pci_add_cap_save_buffer - allocate buffer for saving given
  2512. * capability registers
  2513. * @dev: the PCI device
  2514. * @cap: the capability to allocate the buffer for
  2515. * @extended: Standard or Extended capability ID
  2516. * @size: requested size of the buffer
  2517. */
  2518. static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
  2519. bool extended, unsigned int size)
  2520. {
  2521. int pos;
  2522. struct pci_cap_saved_state *save_state;
  2523. if (extended)
  2524. pos = pci_find_ext_capability(dev, cap);
  2525. else
  2526. pos = pci_find_capability(dev, cap);
  2527. if (!pos)
  2528. return 0;
  2529. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  2530. if (!save_state)
  2531. return -ENOMEM;
  2532. save_state->cap.cap_nr = cap;
  2533. save_state->cap.cap_extended = extended;
  2534. save_state->cap.size = size;
  2535. pci_add_saved_cap(dev, save_state);
  2536. return 0;
  2537. }
  2538. int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
  2539. {
  2540. return _pci_add_cap_save_buffer(dev, cap, false, size);
  2541. }
  2542. int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
  2543. {
  2544. return _pci_add_cap_save_buffer(dev, cap, true, size);
  2545. }
  2546. /**
  2547. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  2548. * @dev: the PCI device
  2549. */
  2550. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  2551. {
  2552. int error;
  2553. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP,
  2554. PCI_EXP_SAVE_REGS * sizeof(u16));
  2555. if (error)
  2556. pci_err(dev, "unable to preallocate PCI Express save buffer\n");
  2557. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  2558. if (error)
  2559. pci_err(dev, "unable to preallocate PCI-X save buffer\n");
  2560. pci_allocate_vc_save_buffers(dev);
  2561. }
  2562. void pci_free_cap_save_buffers(struct pci_dev *dev)
  2563. {
  2564. struct pci_cap_saved_state *tmp;
  2565. struct hlist_node *n;
  2566. hlist_for_each_entry_safe(tmp, n, &dev->saved_cap_space, next)
  2567. kfree(tmp);
  2568. }
  2569. /**
  2570. * pci_configure_ari - enable or disable ARI forwarding
  2571. * @dev: the PCI device
  2572. *
  2573. * If @dev and its upstream bridge both support ARI, enable ARI in the
  2574. * bridge. Otherwise, disable ARI in the bridge.
  2575. */
  2576. void pci_configure_ari(struct pci_dev *dev)
  2577. {
  2578. u32 cap;
  2579. struct pci_dev *bridge;
  2580. if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
  2581. return;
  2582. bridge = dev->bus->self;
  2583. if (!bridge)
  2584. return;
  2585. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2586. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  2587. return;
  2588. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
  2589. pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
  2590. PCI_EXP_DEVCTL2_ARI);
  2591. bridge->ari_enabled = 1;
  2592. } else {
  2593. pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
  2594. PCI_EXP_DEVCTL2_ARI);
  2595. bridge->ari_enabled = 0;
  2596. }
  2597. }
  2598. static int pci_acs_enable;
  2599. /**
  2600. * pci_request_acs - ask for ACS to be enabled if supported
  2601. */
  2602. void pci_request_acs(void)
  2603. {
  2604. pci_acs_enable = 1;
  2605. }
  2606. static const char *disable_acs_redir_param;
  2607. /**
  2608. * pci_disable_acs_redir - disable ACS redirect capabilities
  2609. * @dev: the PCI device
  2610. *
  2611. * For only devices specified in the disable_acs_redir parameter.
  2612. */
  2613. static void pci_disable_acs_redir(struct pci_dev *dev)
  2614. {
  2615. int ret = 0;
  2616. const char *p;
  2617. int pos;
  2618. u16 ctrl;
  2619. if (!disable_acs_redir_param)
  2620. return;
  2621. p = disable_acs_redir_param;
  2622. while (*p) {
  2623. ret = pci_dev_str_match(dev, p, &p);
  2624. if (ret < 0) {
  2625. pr_info_once("PCI: Can't parse disable_acs_redir parameter: %s\n",
  2626. disable_acs_redir_param);
  2627. break;
  2628. } else if (ret == 1) {
  2629. /* Found a match */
  2630. break;
  2631. }
  2632. if (*p != ';' && *p != ',') {
  2633. /* End of param or invalid format */
  2634. break;
  2635. }
  2636. p++;
  2637. }
  2638. if (ret != 1)
  2639. return;
  2640. if (!pci_dev_specific_disable_acs_redir(dev))
  2641. return;
  2642. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2643. if (!pos) {
  2644. pci_warn(dev, "cannot disable ACS redirect for this hardware as it does not have ACS capabilities\n");
  2645. return;
  2646. }
  2647. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2648. /* P2P Request & Completion Redirect */
  2649. ctrl &= ~(PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_EC);
  2650. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2651. pci_info(dev, "disabled ACS redirect\n");
  2652. }
  2653. /**
  2654. * pci_std_enable_acs - enable ACS on devices using standard ACS capabilites
  2655. * @dev: the PCI device
  2656. */
  2657. static void pci_std_enable_acs(struct pci_dev *dev)
  2658. {
  2659. int pos;
  2660. u16 cap;
  2661. u16 ctrl;
  2662. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS);
  2663. if (!pos)
  2664. return;
  2665. pci_read_config_word(dev, pos + PCI_ACS_CAP, &cap);
  2666. pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl);
  2667. /* Source Validation */
  2668. ctrl |= (cap & PCI_ACS_SV);
  2669. /* P2P Request Redirect */
  2670. ctrl |= (cap & PCI_ACS_RR);
  2671. /* P2P Completion Redirect */
  2672. ctrl |= (cap & PCI_ACS_CR);
  2673. /* Upstream Forwarding */
  2674. ctrl |= (cap & PCI_ACS_UF);
  2675. pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl);
  2676. }
  2677. /**
  2678. * pci_enable_acs - enable ACS if hardware support it
  2679. * @dev: the PCI device
  2680. */
  2681. void pci_enable_acs(struct pci_dev *dev)
  2682. {
  2683. if (!pci_acs_enable)
  2684. goto disable_acs_redir;
  2685. if (!pci_dev_specific_enable_acs(dev))
  2686. goto disable_acs_redir;
  2687. pci_std_enable_acs(dev);
  2688. disable_acs_redir:
  2689. /*
  2690. * Note: pci_disable_acs_redir() must be called even if ACS was not
  2691. * enabled by the kernel because it may have been enabled by
  2692. * platform firmware. So if we are told to disable it, we should
  2693. * always disable it after setting the kernel's default
  2694. * preferences.
  2695. */
  2696. pci_disable_acs_redir(dev);
  2697. }
  2698. static bool pci_acs_flags_enabled(struct pci_dev *pdev, u16 acs_flags)
  2699. {
  2700. int pos;
  2701. u16 cap, ctrl;
  2702. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ACS);
  2703. if (!pos)
  2704. return false;
  2705. /*
  2706. * Except for egress control, capabilities are either required
  2707. * or only required if controllable. Features missing from the
  2708. * capability field can therefore be assumed as hard-wired enabled.
  2709. */
  2710. pci_read_config_word(pdev, pos + PCI_ACS_CAP, &cap);
  2711. acs_flags &= (cap | PCI_ACS_EC);
  2712. pci_read_config_word(pdev, pos + PCI_ACS_CTRL, &ctrl);
  2713. return (ctrl & acs_flags) == acs_flags;
  2714. }
  2715. /**
  2716. * pci_acs_enabled - test ACS against required flags for a given device
  2717. * @pdev: device to test
  2718. * @acs_flags: required PCI ACS flags
  2719. *
  2720. * Return true if the device supports the provided flags. Automatically
  2721. * filters out flags that are not implemented on multifunction devices.
  2722. *
  2723. * Note that this interface checks the effective ACS capabilities of the
  2724. * device rather than the actual capabilities. For instance, most single
  2725. * function endpoints are not required to support ACS because they have no
  2726. * opportunity for peer-to-peer access. We therefore return 'true'
  2727. * regardless of whether the device exposes an ACS capability. This makes
  2728. * it much easier for callers of this function to ignore the actual type
  2729. * or topology of the device when testing ACS support.
  2730. */
  2731. bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags)
  2732. {
  2733. int ret;
  2734. ret = pci_dev_specific_acs_enabled(pdev, acs_flags);
  2735. if (ret >= 0)
  2736. return ret > 0;
  2737. /*
  2738. * Conventional PCI and PCI-X devices never support ACS, either
  2739. * effectively or actually. The shared bus topology implies that
  2740. * any device on the bus can receive or snoop DMA.
  2741. */
  2742. if (!pci_is_pcie(pdev))
  2743. return false;
  2744. switch (pci_pcie_type(pdev)) {
  2745. /*
  2746. * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
  2747. * but since their primary interface is PCI/X, we conservatively
  2748. * handle them as we would a non-PCIe device.
  2749. */
  2750. case PCI_EXP_TYPE_PCIE_BRIDGE:
  2751. /*
  2752. * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
  2753. * applicable... must never implement an ACS Extended Capability...".
  2754. * This seems arbitrary, but we take a conservative interpretation
  2755. * of this statement.
  2756. */
  2757. case PCI_EXP_TYPE_PCI_BRIDGE:
  2758. case PCI_EXP_TYPE_RC_EC:
  2759. return false;
  2760. /*
  2761. * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
  2762. * implement ACS in order to indicate their peer-to-peer capabilities,
  2763. * regardless of whether they are single- or multi-function devices.
  2764. */
  2765. case PCI_EXP_TYPE_DOWNSTREAM:
  2766. case PCI_EXP_TYPE_ROOT_PORT:
  2767. return pci_acs_flags_enabled(pdev, acs_flags);
  2768. /*
  2769. * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
  2770. * implemented by the remaining PCIe types to indicate peer-to-peer
  2771. * capabilities, but only when they are part of a multifunction
  2772. * device. The footnote for section 6.12 indicates the specific
  2773. * PCIe types included here.
  2774. */
  2775. case PCI_EXP_TYPE_ENDPOINT:
  2776. case PCI_EXP_TYPE_UPSTREAM:
  2777. case PCI_EXP_TYPE_LEG_END:
  2778. case PCI_EXP_TYPE_RC_END:
  2779. if (!pdev->multifunction)
  2780. break;
  2781. return pci_acs_flags_enabled(pdev, acs_flags);
  2782. }
  2783. /*
  2784. * PCIe 3.0, 6.12.1.3 specifies no ACS capabilities are applicable
  2785. * to single function devices with the exception of downstream ports.
  2786. */
  2787. return true;
  2788. }
  2789. /**
  2790. * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
  2791. * @start: starting downstream device
  2792. * @end: ending upstream device or NULL to search to the root bus
  2793. * @acs_flags: required flags
  2794. *
  2795. * Walk up a device tree from start to end testing PCI ACS support. If
  2796. * any step along the way does not support the required flags, return false.
  2797. */
  2798. bool pci_acs_path_enabled(struct pci_dev *start,
  2799. struct pci_dev *end, u16 acs_flags)
  2800. {
  2801. struct pci_dev *pdev, *parent = start;
  2802. do {
  2803. pdev = parent;
  2804. if (!pci_acs_enabled(pdev, acs_flags))
  2805. return false;
  2806. if (pci_is_root_bus(pdev->bus))
  2807. return (end == NULL);
  2808. parent = pdev->bus->self;
  2809. } while (pdev != end);
  2810. return true;
  2811. }
  2812. /**
  2813. * pci_rebar_find_pos - find position of resize ctrl reg for BAR
  2814. * @pdev: PCI device
  2815. * @bar: BAR to find
  2816. *
  2817. * Helper to find the position of the ctrl register for a BAR.
  2818. * Returns -ENOTSUPP if resizable BARs are not supported at all.
  2819. * Returns -ENOENT if no ctrl register for the BAR could be found.
  2820. */
  2821. static int pci_rebar_find_pos(struct pci_dev *pdev, int bar)
  2822. {
  2823. unsigned int pos, nbars, i;
  2824. u32 ctrl;
  2825. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR);
  2826. if (!pos)
  2827. return -ENOTSUPP;
  2828. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2829. nbars = (ctrl & PCI_REBAR_CTRL_NBAR_MASK) >>
  2830. PCI_REBAR_CTRL_NBAR_SHIFT;
  2831. for (i = 0; i < nbars; i++, pos += 8) {
  2832. int bar_idx;
  2833. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2834. bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX;
  2835. if (bar_idx == bar)
  2836. return pos;
  2837. }
  2838. return -ENOENT;
  2839. }
  2840. /**
  2841. * pci_rebar_get_possible_sizes - get possible sizes for BAR
  2842. * @pdev: PCI device
  2843. * @bar: BAR to query
  2844. *
  2845. * Get the possible sizes of a resizable BAR as bitmask defined in the spec
  2846. * (bit 0=1MB, bit 19=512GB). Returns 0 if BAR isn't resizable.
  2847. */
  2848. u32 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
  2849. {
  2850. int pos;
  2851. u32 cap;
  2852. pos = pci_rebar_find_pos(pdev, bar);
  2853. if (pos < 0)
  2854. return 0;
  2855. pci_read_config_dword(pdev, pos + PCI_REBAR_CAP, &cap);
  2856. return (cap & PCI_REBAR_CAP_SIZES) >> 4;
  2857. }
  2858. /**
  2859. * pci_rebar_get_current_size - get the current size of a BAR
  2860. * @pdev: PCI device
  2861. * @bar: BAR to set size to
  2862. *
  2863. * Read the size of a BAR from the resizable BAR config.
  2864. * Returns size if found or negative error code.
  2865. */
  2866. int pci_rebar_get_current_size(struct pci_dev *pdev, int bar)
  2867. {
  2868. int pos;
  2869. u32 ctrl;
  2870. pos = pci_rebar_find_pos(pdev, bar);
  2871. if (pos < 0)
  2872. return pos;
  2873. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2874. return (ctrl & PCI_REBAR_CTRL_BAR_SIZE) >> PCI_REBAR_CTRL_BAR_SHIFT;
  2875. }
  2876. /**
  2877. * pci_rebar_set_size - set a new size for a BAR
  2878. * @pdev: PCI device
  2879. * @bar: BAR to set size to
  2880. * @size: new size as defined in the spec (0=1MB, 19=512GB)
  2881. *
  2882. * Set the new size of a BAR as defined in the spec.
  2883. * Returns zero if resizing was successful, error code otherwise.
  2884. */
  2885. int pci_rebar_set_size(struct pci_dev *pdev, int bar, int size)
  2886. {
  2887. int pos;
  2888. u32 ctrl;
  2889. pos = pci_rebar_find_pos(pdev, bar);
  2890. if (pos < 0)
  2891. return pos;
  2892. pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl);
  2893. ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE;
  2894. ctrl |= size << PCI_REBAR_CTRL_BAR_SHIFT;
  2895. pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl);
  2896. return 0;
  2897. }
  2898. /**
  2899. * pci_enable_atomic_ops_to_root - enable AtomicOp requests to root port
  2900. * @dev: the PCI device
  2901. * @cap_mask: mask of desired AtomicOp sizes, including one or more of:
  2902. * PCI_EXP_DEVCAP2_ATOMIC_COMP32
  2903. * PCI_EXP_DEVCAP2_ATOMIC_COMP64
  2904. * PCI_EXP_DEVCAP2_ATOMIC_COMP128
  2905. *
  2906. * Return 0 if all upstream bridges support AtomicOp routing, egress
  2907. * blocking is disabled on all upstream ports, and the root port supports
  2908. * the requested completion capabilities (32-bit, 64-bit and/or 128-bit
  2909. * AtomicOp completion), or negative otherwise.
  2910. */
  2911. int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
  2912. {
  2913. struct pci_bus *bus = dev->bus;
  2914. struct pci_dev *bridge;
  2915. u32 cap, ctl2;
  2916. if (!pci_is_pcie(dev))
  2917. return -EINVAL;
  2918. /*
  2919. * Per PCIe r4.0, sec 6.15, endpoints and root ports may be
  2920. * AtomicOp requesters. For now, we only support endpoints as
  2921. * requesters and root ports as completers. No endpoints as
  2922. * completers, and no peer-to-peer.
  2923. */
  2924. switch (pci_pcie_type(dev)) {
  2925. case PCI_EXP_TYPE_ENDPOINT:
  2926. case PCI_EXP_TYPE_LEG_END:
  2927. case PCI_EXP_TYPE_RC_END:
  2928. break;
  2929. default:
  2930. return -EINVAL;
  2931. }
  2932. while (bus->parent) {
  2933. bridge = bus->self;
  2934. pcie_capability_read_dword(bridge, PCI_EXP_DEVCAP2, &cap);
  2935. switch (pci_pcie_type(bridge)) {
  2936. /* Ensure switch ports support AtomicOp routing */
  2937. case PCI_EXP_TYPE_UPSTREAM:
  2938. case PCI_EXP_TYPE_DOWNSTREAM:
  2939. if (!(cap & PCI_EXP_DEVCAP2_ATOMIC_ROUTE))
  2940. return -EINVAL;
  2941. break;
  2942. /* Ensure root port supports all the sizes we care about */
  2943. case PCI_EXP_TYPE_ROOT_PORT:
  2944. if ((cap & cap_mask) != cap_mask)
  2945. return -EINVAL;
  2946. break;
  2947. }
  2948. /* Ensure upstream ports don't block AtomicOps on egress */
  2949. if (!bridge->has_secondary_link) {
  2950. pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2,
  2951. &ctl2);
  2952. if (ctl2 & PCI_EXP_DEVCTL2_ATOMIC_EGRESS_BLOCK)
  2953. return -EINVAL;
  2954. }
  2955. bus = bus->parent;
  2956. }
  2957. pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
  2958. PCI_EXP_DEVCTL2_ATOMIC_REQ);
  2959. return 0;
  2960. }
  2961. EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
  2962. /**
  2963. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  2964. * @dev: the PCI device
  2965. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  2966. *
  2967. * Perform INTx swizzling for a device behind one level of bridge. This is
  2968. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  2969. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  2970. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  2971. * the PCI Express Base Specification, Revision 2.1)
  2972. */
  2973. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  2974. {
  2975. int slot;
  2976. if (pci_ari_enabled(dev->bus))
  2977. slot = 0;
  2978. else
  2979. slot = PCI_SLOT(dev->devfn);
  2980. return (((pin - 1) + slot) % 4) + 1;
  2981. }
  2982. int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  2983. {
  2984. u8 pin;
  2985. pin = dev->pin;
  2986. if (!pin)
  2987. return -1;
  2988. while (!pci_is_root_bus(dev->bus)) {
  2989. pin = pci_swizzle_interrupt_pin(dev, pin);
  2990. dev = dev->bus->self;
  2991. }
  2992. *bridge = dev;
  2993. return pin;
  2994. }
  2995. /**
  2996. * pci_common_swizzle - swizzle INTx all the way to root bridge
  2997. * @dev: the PCI device
  2998. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  2999. *
  3000. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  3001. * bridges all the way up to a PCI root bus.
  3002. */
  3003. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  3004. {
  3005. u8 pin = *pinp;
  3006. while (!pci_is_root_bus(dev->bus)) {
  3007. pin = pci_swizzle_interrupt_pin(dev, pin);
  3008. dev = dev->bus->self;
  3009. }
  3010. *pinp = pin;
  3011. return PCI_SLOT(dev->devfn);
  3012. }
  3013. EXPORT_SYMBOL_GPL(pci_common_swizzle);
  3014. /**
  3015. * pci_release_region - Release a PCI bar
  3016. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  3017. * @bar: BAR to release
  3018. *
  3019. * Releases the PCI I/O and memory resources previously reserved by a
  3020. * successful call to pci_request_region. Call this function only
  3021. * after all use of the PCI regions has ceased.
  3022. */
  3023. void pci_release_region(struct pci_dev *pdev, int bar)
  3024. {
  3025. struct pci_devres *dr;
  3026. if (pci_resource_len(pdev, bar) == 0)
  3027. return;
  3028. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  3029. release_region(pci_resource_start(pdev, bar),
  3030. pci_resource_len(pdev, bar));
  3031. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  3032. release_mem_region(pci_resource_start(pdev, bar),
  3033. pci_resource_len(pdev, bar));
  3034. dr = find_pci_dr(pdev);
  3035. if (dr)
  3036. dr->region_mask &= ~(1 << bar);
  3037. }
  3038. EXPORT_SYMBOL(pci_release_region);
  3039. /**
  3040. * __pci_request_region - Reserved PCI I/O and memory resource
  3041. * @pdev: PCI device whose resources are to be reserved
  3042. * @bar: BAR to be reserved
  3043. * @res_name: Name to be associated with resource.
  3044. * @exclusive: whether the region access is exclusive or not
  3045. *
  3046. * Mark the PCI region associated with PCI device @pdev BR @bar as
  3047. * being reserved by owner @res_name. Do not access any
  3048. * address inside the PCI regions unless this call returns
  3049. * successfully.
  3050. *
  3051. * If @exclusive is set, then the region is marked so that userspace
  3052. * is explicitly not allowed to map the resource via /dev/mem or
  3053. * sysfs MMIO access.
  3054. *
  3055. * Returns 0 on success, or %EBUSY on error. A warning
  3056. * message is also printed on failure.
  3057. */
  3058. static int __pci_request_region(struct pci_dev *pdev, int bar,
  3059. const char *res_name, int exclusive)
  3060. {
  3061. struct pci_devres *dr;
  3062. if (pci_resource_len(pdev, bar) == 0)
  3063. return 0;
  3064. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  3065. if (!request_region(pci_resource_start(pdev, bar),
  3066. pci_resource_len(pdev, bar), res_name))
  3067. goto err_out;
  3068. } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  3069. if (!__request_mem_region(pci_resource_start(pdev, bar),
  3070. pci_resource_len(pdev, bar), res_name,
  3071. exclusive))
  3072. goto err_out;
  3073. }
  3074. dr = find_pci_dr(pdev);
  3075. if (dr)
  3076. dr->region_mask |= 1 << bar;
  3077. return 0;
  3078. err_out:
  3079. pci_warn(pdev, "BAR %d: can't reserve %pR\n", bar,
  3080. &pdev->resource[bar]);
  3081. return -EBUSY;
  3082. }
  3083. /**
  3084. * pci_request_region - Reserve PCI I/O and memory resource
  3085. * @pdev: PCI device whose resources are to be reserved
  3086. * @bar: BAR to be reserved
  3087. * @res_name: Name to be associated with resource
  3088. *
  3089. * Mark the PCI region associated with PCI device @pdev BAR @bar as
  3090. * being reserved by owner @res_name. Do not access any
  3091. * address inside the PCI regions unless this call returns
  3092. * successfully.
  3093. *
  3094. * Returns 0 on success, or %EBUSY on error. A warning
  3095. * message is also printed on failure.
  3096. */
  3097. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  3098. {
  3099. return __pci_request_region(pdev, bar, res_name, 0);
  3100. }
  3101. EXPORT_SYMBOL(pci_request_region);
  3102. /**
  3103. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  3104. * @pdev: PCI device whose resources are to be reserved
  3105. * @bar: BAR to be reserved
  3106. * @res_name: Name to be associated with resource.
  3107. *
  3108. * Mark the PCI region associated with PCI device @pdev BR @bar as
  3109. * being reserved by owner @res_name. Do not access any
  3110. * address inside the PCI regions unless this call returns
  3111. * successfully.
  3112. *
  3113. * Returns 0 on success, or %EBUSY on error. A warning
  3114. * message is also printed on failure.
  3115. *
  3116. * The key difference that _exclusive makes it that userspace is
  3117. * explicitly not allowed to map the resource via /dev/mem or
  3118. * sysfs.
  3119. */
  3120. int pci_request_region_exclusive(struct pci_dev *pdev, int bar,
  3121. const char *res_name)
  3122. {
  3123. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  3124. }
  3125. EXPORT_SYMBOL(pci_request_region_exclusive);
  3126. /**
  3127. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  3128. * @pdev: PCI device whose resources were previously reserved
  3129. * @bars: Bitmask of BARs to be released
  3130. *
  3131. * Release selected PCI I/O and memory resources previously reserved.
  3132. * Call this function only after all use of the PCI regions has ceased.
  3133. */
  3134. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  3135. {
  3136. int i;
  3137. for (i = 0; i < 6; i++)
  3138. if (bars & (1 << i))
  3139. pci_release_region(pdev, i);
  3140. }
  3141. EXPORT_SYMBOL(pci_release_selected_regions);
  3142. static int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3143. const char *res_name, int excl)
  3144. {
  3145. int i;
  3146. for (i = 0; i < 6; i++)
  3147. if (bars & (1 << i))
  3148. if (__pci_request_region(pdev, i, res_name, excl))
  3149. goto err_out;
  3150. return 0;
  3151. err_out:
  3152. while (--i >= 0)
  3153. if (bars & (1 << i))
  3154. pci_release_region(pdev, i);
  3155. return -EBUSY;
  3156. }
  3157. /**
  3158. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  3159. * @pdev: PCI device whose resources are to be reserved
  3160. * @bars: Bitmask of BARs to be requested
  3161. * @res_name: Name to be associated with resource
  3162. */
  3163. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  3164. const char *res_name)
  3165. {
  3166. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  3167. }
  3168. EXPORT_SYMBOL(pci_request_selected_regions);
  3169. int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars,
  3170. const char *res_name)
  3171. {
  3172. return __pci_request_selected_regions(pdev, bars, res_name,
  3173. IORESOURCE_EXCLUSIVE);
  3174. }
  3175. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  3176. /**
  3177. * pci_release_regions - Release reserved PCI I/O and memory resources
  3178. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  3179. *
  3180. * Releases all PCI I/O and memory resources previously reserved by a
  3181. * successful call to pci_request_regions. Call this function only
  3182. * after all use of the PCI regions has ceased.
  3183. */
  3184. void pci_release_regions(struct pci_dev *pdev)
  3185. {
  3186. pci_release_selected_regions(pdev, (1 << 6) - 1);
  3187. }
  3188. EXPORT_SYMBOL(pci_release_regions);
  3189. /**
  3190. * pci_request_regions - Reserved PCI I/O and memory resources
  3191. * @pdev: PCI device whose resources are to be reserved
  3192. * @res_name: Name to be associated with resource.
  3193. *
  3194. * Mark all PCI regions associated with PCI device @pdev as
  3195. * being reserved by owner @res_name. Do not access any
  3196. * address inside the PCI regions unless this call returns
  3197. * successfully.
  3198. *
  3199. * Returns 0 on success, or %EBUSY on error. A warning
  3200. * message is also printed on failure.
  3201. */
  3202. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  3203. {
  3204. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  3205. }
  3206. EXPORT_SYMBOL(pci_request_regions);
  3207. /**
  3208. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  3209. * @pdev: PCI device whose resources are to be reserved
  3210. * @res_name: Name to be associated with resource.
  3211. *
  3212. * Mark all PCI regions associated with PCI device @pdev as
  3213. * being reserved by owner @res_name. Do not access any
  3214. * address inside the PCI regions unless this call returns
  3215. * successfully.
  3216. *
  3217. * pci_request_regions_exclusive() will mark the region so that
  3218. * /dev/mem and the sysfs MMIO access will not be allowed.
  3219. *
  3220. * Returns 0 on success, or %EBUSY on error. A warning
  3221. * message is also printed on failure.
  3222. */
  3223. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  3224. {
  3225. return pci_request_selected_regions_exclusive(pdev,
  3226. ((1 << 6) - 1), res_name);
  3227. }
  3228. EXPORT_SYMBOL(pci_request_regions_exclusive);
  3229. /*
  3230. * Record the PCI IO range (expressed as CPU physical address + size).
  3231. * Return a negative value if an error has occured, zero otherwise
  3232. */
  3233. int pci_register_io_range(struct fwnode_handle *fwnode, phys_addr_t addr,
  3234. resource_size_t size)
  3235. {
  3236. int ret = 0;
  3237. #ifdef PCI_IOBASE
  3238. struct logic_pio_hwaddr *range;
  3239. if (!size || addr + size < addr)
  3240. return -EINVAL;
  3241. range = kzalloc(sizeof(*range), GFP_ATOMIC);
  3242. if (!range)
  3243. return -ENOMEM;
  3244. range->fwnode = fwnode;
  3245. range->size = size;
  3246. range->hw_start = addr;
  3247. range->flags = LOGIC_PIO_CPU_MMIO;
  3248. ret = logic_pio_register_range(range);
  3249. if (ret)
  3250. kfree(range);
  3251. #endif
  3252. return ret;
  3253. }
  3254. phys_addr_t pci_pio_to_address(unsigned long pio)
  3255. {
  3256. phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
  3257. #ifdef PCI_IOBASE
  3258. if (pio >= MMIO_UPPER_LIMIT)
  3259. return address;
  3260. address = logic_pio_to_hwaddr(pio);
  3261. #endif
  3262. return address;
  3263. }
  3264. unsigned long __weak pci_address_to_pio(phys_addr_t address)
  3265. {
  3266. #ifdef PCI_IOBASE
  3267. return logic_pio_trans_cpuaddr(address);
  3268. #else
  3269. if (address > IO_SPACE_LIMIT)
  3270. return (unsigned long)-1;
  3271. return (unsigned long) address;
  3272. #endif
  3273. }
  3274. /**
  3275. * pci_remap_iospace - Remap the memory mapped I/O space
  3276. * @res: Resource describing the I/O space
  3277. * @phys_addr: physical address of range to be mapped
  3278. *
  3279. * Remap the memory mapped I/O space described by the @res
  3280. * and the CPU physical address @phys_addr into virtual address space.
  3281. * Only architectures that have memory mapped IO functions defined
  3282. * (and the PCI_IOBASE value defined) should call this function.
  3283. */
  3284. int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
  3285. {
  3286. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3287. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3288. if (!(res->flags & IORESOURCE_IO))
  3289. return -EINVAL;
  3290. if (res->end > IO_SPACE_LIMIT)
  3291. return -EINVAL;
  3292. return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
  3293. pgprot_device(PAGE_KERNEL));
  3294. #else
  3295. /* this architecture does not have memory mapped I/O space,
  3296. so this function should never be called */
  3297. WARN_ONCE(1, "This architecture does not support memory mapped I/O\n");
  3298. return -ENODEV;
  3299. #endif
  3300. }
  3301. EXPORT_SYMBOL(pci_remap_iospace);
  3302. /**
  3303. * pci_unmap_iospace - Unmap the memory mapped I/O space
  3304. * @res: resource to be unmapped
  3305. *
  3306. * Unmap the CPU virtual address @res from virtual address space.
  3307. * Only architectures that have memory mapped IO functions defined
  3308. * (and the PCI_IOBASE value defined) should call this function.
  3309. */
  3310. void pci_unmap_iospace(struct resource *res)
  3311. {
  3312. #if defined(PCI_IOBASE) && defined(CONFIG_MMU)
  3313. unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
  3314. unmap_kernel_range(vaddr, resource_size(res));
  3315. #endif
  3316. }
  3317. EXPORT_SYMBOL(pci_unmap_iospace);
  3318. static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
  3319. {
  3320. struct resource **res = ptr;
  3321. pci_unmap_iospace(*res);
  3322. }
  3323. /**
  3324. * devm_pci_remap_iospace - Managed pci_remap_iospace()
  3325. * @dev: Generic device to remap IO address for
  3326. * @res: Resource describing the I/O space
  3327. * @phys_addr: physical address of range to be mapped
  3328. *
  3329. * Managed pci_remap_iospace(). Map is automatically unmapped on driver
  3330. * detach.
  3331. */
  3332. int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
  3333. phys_addr_t phys_addr)
  3334. {
  3335. const struct resource **ptr;
  3336. int error;
  3337. ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
  3338. if (!ptr)
  3339. return -ENOMEM;
  3340. error = pci_remap_iospace(res, phys_addr);
  3341. if (error) {
  3342. devres_free(ptr);
  3343. } else {
  3344. *ptr = res;
  3345. devres_add(dev, ptr);
  3346. }
  3347. return error;
  3348. }
  3349. EXPORT_SYMBOL(devm_pci_remap_iospace);
  3350. /**
  3351. * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
  3352. * @dev: Generic device to remap IO address for
  3353. * @offset: Resource address to map
  3354. * @size: Size of map
  3355. *
  3356. * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver
  3357. * detach.
  3358. */
  3359. void __iomem *devm_pci_remap_cfgspace(struct device *dev,
  3360. resource_size_t offset,
  3361. resource_size_t size)
  3362. {
  3363. void __iomem **ptr, *addr;
  3364. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  3365. if (!ptr)
  3366. return NULL;
  3367. addr = pci_remap_cfgspace(offset, size);
  3368. if (addr) {
  3369. *ptr = addr;
  3370. devres_add(dev, ptr);
  3371. } else
  3372. devres_free(ptr);
  3373. return addr;
  3374. }
  3375. EXPORT_SYMBOL(devm_pci_remap_cfgspace);
  3376. /**
  3377. * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
  3378. * @dev: generic device to handle the resource for
  3379. * @res: configuration space resource to be handled
  3380. *
  3381. * Checks that a resource is a valid memory region, requests the memory
  3382. * region and ioremaps with pci_remap_cfgspace() API that ensures the
  3383. * proper PCI configuration space memory attributes are guaranteed.
  3384. *
  3385. * All operations are managed and will be undone on driver detach.
  3386. *
  3387. * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
  3388. * on failure. Usage example::
  3389. *
  3390. * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3391. * base = devm_pci_remap_cfg_resource(&pdev->dev, res);
  3392. * if (IS_ERR(base))
  3393. * return PTR_ERR(base);
  3394. */
  3395. void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
  3396. struct resource *res)
  3397. {
  3398. resource_size_t size;
  3399. const char *name;
  3400. void __iomem *dest_ptr;
  3401. BUG_ON(!dev);
  3402. if (!res || resource_type(res) != IORESOURCE_MEM) {
  3403. dev_err(dev, "invalid resource\n");
  3404. return IOMEM_ERR_PTR(-EINVAL);
  3405. }
  3406. size = resource_size(res);
  3407. name = res->name ?: dev_name(dev);
  3408. if (!devm_request_mem_region(dev, res->start, size, name)) {
  3409. dev_err(dev, "can't request region for resource %pR\n", res);
  3410. return IOMEM_ERR_PTR(-EBUSY);
  3411. }
  3412. dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
  3413. if (!dest_ptr) {
  3414. dev_err(dev, "ioremap failed for resource %pR\n", res);
  3415. devm_release_mem_region(dev, res->start, size);
  3416. dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
  3417. }
  3418. return dest_ptr;
  3419. }
  3420. EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
  3421. static void __pci_set_master(struct pci_dev *dev, bool enable)
  3422. {
  3423. u16 old_cmd, cmd;
  3424. pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  3425. if (enable)
  3426. cmd = old_cmd | PCI_COMMAND_MASTER;
  3427. else
  3428. cmd = old_cmd & ~PCI_COMMAND_MASTER;
  3429. if (cmd != old_cmd) {
  3430. pci_dbg(dev, "%s bus mastering\n",
  3431. enable ? "enabling" : "disabling");
  3432. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3433. }
  3434. dev->is_busmaster = enable;
  3435. }
  3436. /**
  3437. * pcibios_setup - process "pci=" kernel boot arguments
  3438. * @str: string used to pass in "pci=" kernel boot arguments
  3439. *
  3440. * Process kernel boot arguments. This is the default implementation.
  3441. * Architecture specific implementations can override this as necessary.
  3442. */
  3443. char * __weak __init pcibios_setup(char *str)
  3444. {
  3445. return str;
  3446. }
  3447. /**
  3448. * pcibios_set_master - enable PCI bus-mastering for device dev
  3449. * @dev: the PCI device to enable
  3450. *
  3451. * Enables PCI bus-mastering for the device. This is the default
  3452. * implementation. Architecture specific implementations can override
  3453. * this if necessary.
  3454. */
  3455. void __weak pcibios_set_master(struct pci_dev *dev)
  3456. {
  3457. u8 lat;
  3458. /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  3459. if (pci_is_pcie(dev))
  3460. return;
  3461. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3462. if (lat < 16)
  3463. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  3464. else if (lat > pcibios_max_latency)
  3465. lat = pcibios_max_latency;
  3466. else
  3467. return;
  3468. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  3469. }
  3470. /**
  3471. * pci_set_master - enables bus-mastering for device dev
  3472. * @dev: the PCI device to enable
  3473. *
  3474. * Enables bus-mastering on the device and calls pcibios_set_master()
  3475. * to do the needed arch specific settings.
  3476. */
  3477. void pci_set_master(struct pci_dev *dev)
  3478. {
  3479. __pci_set_master(dev, true);
  3480. pcibios_set_master(dev);
  3481. }
  3482. EXPORT_SYMBOL(pci_set_master);
  3483. /**
  3484. * pci_clear_master - disables bus-mastering for device dev
  3485. * @dev: the PCI device to disable
  3486. */
  3487. void pci_clear_master(struct pci_dev *dev)
  3488. {
  3489. __pci_set_master(dev, false);
  3490. }
  3491. EXPORT_SYMBOL(pci_clear_master);
  3492. /**
  3493. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  3494. * @dev: the PCI device for which MWI is to be enabled
  3495. *
  3496. * Helper function for pci_set_mwi.
  3497. * Originally copied from drivers/net/acenic.c.
  3498. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  3499. *
  3500. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3501. */
  3502. int pci_set_cacheline_size(struct pci_dev *dev)
  3503. {
  3504. u8 cacheline_size;
  3505. if (!pci_cache_line_size)
  3506. return -EINVAL;
  3507. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  3508. equal to or multiple of the right value. */
  3509. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3510. if (cacheline_size >= pci_cache_line_size &&
  3511. (cacheline_size % pci_cache_line_size) == 0)
  3512. return 0;
  3513. /* Write the correct value. */
  3514. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  3515. /* Read it back. */
  3516. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  3517. if (cacheline_size == pci_cache_line_size)
  3518. return 0;
  3519. pci_printk(KERN_DEBUG, dev, "cache line size of %d is not supported\n",
  3520. pci_cache_line_size << 2);
  3521. return -EINVAL;
  3522. }
  3523. EXPORT_SYMBOL_GPL(pci_set_cacheline_size);
  3524. /**
  3525. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  3526. * @dev: the PCI device for which MWI is enabled
  3527. *
  3528. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3529. *
  3530. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3531. */
  3532. int pci_set_mwi(struct pci_dev *dev)
  3533. {
  3534. #ifdef PCI_DISABLE_MWI
  3535. return 0;
  3536. #else
  3537. int rc;
  3538. u16 cmd;
  3539. rc = pci_set_cacheline_size(dev);
  3540. if (rc)
  3541. return rc;
  3542. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3543. if (!(cmd & PCI_COMMAND_INVALIDATE)) {
  3544. pci_dbg(dev, "enabling Mem-Wr-Inval\n");
  3545. cmd |= PCI_COMMAND_INVALIDATE;
  3546. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3547. }
  3548. return 0;
  3549. #endif
  3550. }
  3551. EXPORT_SYMBOL(pci_set_mwi);
  3552. /**
  3553. * pcim_set_mwi - a device-managed pci_set_mwi()
  3554. * @dev: the PCI device for which MWI is enabled
  3555. *
  3556. * Managed pci_set_mwi().
  3557. *
  3558. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3559. */
  3560. int pcim_set_mwi(struct pci_dev *dev)
  3561. {
  3562. struct pci_devres *dr;
  3563. dr = find_pci_dr(dev);
  3564. if (!dr)
  3565. return -ENOMEM;
  3566. dr->mwi = 1;
  3567. return pci_set_mwi(dev);
  3568. }
  3569. EXPORT_SYMBOL(pcim_set_mwi);
  3570. /**
  3571. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  3572. * @dev: the PCI device for which MWI is enabled
  3573. *
  3574. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  3575. * Callers are not required to check the return value.
  3576. *
  3577. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  3578. */
  3579. int pci_try_set_mwi(struct pci_dev *dev)
  3580. {
  3581. #ifdef PCI_DISABLE_MWI
  3582. return 0;
  3583. #else
  3584. return pci_set_mwi(dev);
  3585. #endif
  3586. }
  3587. EXPORT_SYMBOL(pci_try_set_mwi);
  3588. /**
  3589. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  3590. * @dev: the PCI device to disable
  3591. *
  3592. * Disables PCI Memory-Write-Invalidate transaction on the device
  3593. */
  3594. void pci_clear_mwi(struct pci_dev *dev)
  3595. {
  3596. #ifndef PCI_DISABLE_MWI
  3597. u16 cmd;
  3598. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  3599. if (cmd & PCI_COMMAND_INVALIDATE) {
  3600. cmd &= ~PCI_COMMAND_INVALIDATE;
  3601. pci_write_config_word(dev, PCI_COMMAND, cmd);
  3602. }
  3603. #endif
  3604. }
  3605. EXPORT_SYMBOL(pci_clear_mwi);
  3606. /**
  3607. * pci_intx - enables/disables PCI INTx for device dev
  3608. * @pdev: the PCI device to operate on
  3609. * @enable: boolean: whether to enable or disable PCI INTx
  3610. *
  3611. * Enables/disables PCI INTx for device dev
  3612. */
  3613. void pci_intx(struct pci_dev *pdev, int enable)
  3614. {
  3615. u16 pci_command, new;
  3616. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  3617. if (enable)
  3618. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  3619. else
  3620. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  3621. if (new != pci_command) {
  3622. struct pci_devres *dr;
  3623. pci_write_config_word(pdev, PCI_COMMAND, new);
  3624. dr = find_pci_dr(pdev);
  3625. if (dr && !dr->restore_intx) {
  3626. dr->restore_intx = 1;
  3627. dr->orig_intx = !enable;
  3628. }
  3629. }
  3630. }
  3631. EXPORT_SYMBOL_GPL(pci_intx);
  3632. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  3633. {
  3634. struct pci_bus *bus = dev->bus;
  3635. bool mask_updated = true;
  3636. u32 cmd_status_dword;
  3637. u16 origcmd, newcmd;
  3638. unsigned long flags;
  3639. bool irq_pending;
  3640. /*
  3641. * We do a single dword read to retrieve both command and status.
  3642. * Document assumptions that make this possible.
  3643. */
  3644. BUILD_BUG_ON(PCI_COMMAND % 4);
  3645. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  3646. raw_spin_lock_irqsave(&pci_lock, flags);
  3647. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  3648. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  3649. /*
  3650. * Check interrupt status register to see whether our device
  3651. * triggered the interrupt (when masking) or the next IRQ is
  3652. * already pending (when unmasking).
  3653. */
  3654. if (mask != irq_pending) {
  3655. mask_updated = false;
  3656. goto done;
  3657. }
  3658. origcmd = cmd_status_dword;
  3659. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  3660. if (mask)
  3661. newcmd |= PCI_COMMAND_INTX_DISABLE;
  3662. if (newcmd != origcmd)
  3663. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  3664. done:
  3665. raw_spin_unlock_irqrestore(&pci_lock, flags);
  3666. return mask_updated;
  3667. }
  3668. /**
  3669. * pci_check_and_mask_intx - mask INTx on pending interrupt
  3670. * @dev: the PCI device to operate on
  3671. *
  3672. * Check if the device dev has its INTx line asserted, mask it and
  3673. * return true in that case. False is returned if no interrupt was
  3674. * pending.
  3675. */
  3676. bool pci_check_and_mask_intx(struct pci_dev *dev)
  3677. {
  3678. return pci_check_and_set_intx_mask(dev, true);
  3679. }
  3680. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  3681. /**
  3682. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  3683. * @dev: the PCI device to operate on
  3684. *
  3685. * Check if the device dev has its INTx line asserted, unmask it if not
  3686. * and return true. False is returned and the mask remains active if
  3687. * there was still an interrupt pending.
  3688. */
  3689. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  3690. {
  3691. return pci_check_and_set_intx_mask(dev, false);
  3692. }
  3693. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  3694. /**
  3695. * pci_wait_for_pending_transaction - waits for pending transaction
  3696. * @dev: the PCI device to operate on
  3697. *
  3698. * Return 0 if transaction is pending 1 otherwise.
  3699. */
  3700. int pci_wait_for_pending_transaction(struct pci_dev *dev)
  3701. {
  3702. if (!pci_is_pcie(dev))
  3703. return 1;
  3704. return pci_wait_for_pending(dev, pci_pcie_cap(dev) + PCI_EXP_DEVSTA,
  3705. PCI_EXP_DEVSTA_TRPND);
  3706. }
  3707. EXPORT_SYMBOL(pci_wait_for_pending_transaction);
  3708. static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
  3709. {
  3710. int delay = 1;
  3711. u32 id;
  3712. /*
  3713. * After reset, the device should not silently discard config
  3714. * requests, but it may still indicate that it needs more time by
  3715. * responding to them with CRS completions. The Root Port will
  3716. * generally synthesize ~0 data to complete the read (except when
  3717. * CRS SV is enabled and the read was for the Vendor ID; in that
  3718. * case it synthesizes 0x0001 data).
  3719. *
  3720. * Wait for the device to return a non-CRS completion. Read the
  3721. * Command register instead of Vendor ID so we don't have to
  3722. * contend with the CRS SV value.
  3723. */
  3724. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3725. while (id == ~0) {
  3726. if (delay > timeout) {
  3727. pci_warn(dev, "not ready %dms after %s; giving up\n",
  3728. delay - 1, reset_type);
  3729. return -ENOTTY;
  3730. }
  3731. if (delay > 1000)
  3732. pci_info(dev, "not ready %dms after %s; waiting\n",
  3733. delay - 1, reset_type);
  3734. msleep(delay);
  3735. delay *= 2;
  3736. pci_read_config_dword(dev, PCI_COMMAND, &id);
  3737. }
  3738. if (delay > 1000)
  3739. pci_info(dev, "ready %dms after %s\n", delay - 1,
  3740. reset_type);
  3741. return 0;
  3742. }
  3743. /**
  3744. * pcie_has_flr - check if a device supports function level resets
  3745. * @dev: device to check
  3746. *
  3747. * Returns true if the device advertises support for PCIe function level
  3748. * resets.
  3749. */
  3750. bool pcie_has_flr(struct pci_dev *dev)
  3751. {
  3752. u32 cap;
  3753. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3754. return false;
  3755. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  3756. return cap & PCI_EXP_DEVCAP_FLR;
  3757. }
  3758. EXPORT_SYMBOL_GPL(pcie_has_flr);
  3759. /**
  3760. * pcie_flr - initiate a PCIe function level reset
  3761. * @dev: device to reset
  3762. *
  3763. * Initiate a function level reset on @dev. The caller should ensure the
  3764. * device supports FLR before calling this function, e.g. by using the
  3765. * pcie_has_flr() helper.
  3766. */
  3767. int pcie_flr(struct pci_dev *dev)
  3768. {
  3769. if (!pci_wait_for_pending_transaction(dev))
  3770. pci_err(dev, "timed out waiting for pending transaction; performing function level reset anyway\n");
  3771. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  3772. if (dev->imm_ready)
  3773. return 0;
  3774. /*
  3775. * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within
  3776. * 100ms, but may silently discard requests while the FLR is in
  3777. * progress. Wait 100ms before trying to access the device.
  3778. */
  3779. msleep(100);
  3780. return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS);
  3781. }
  3782. EXPORT_SYMBOL_GPL(pcie_flr);
  3783. static int pci_af_flr(struct pci_dev *dev, int probe)
  3784. {
  3785. int pos;
  3786. u8 cap;
  3787. pos = pci_find_capability(dev, PCI_CAP_ID_AF);
  3788. if (!pos)
  3789. return -ENOTTY;
  3790. if (dev->dev_flags & PCI_DEV_FLAGS_NO_FLR_RESET)
  3791. return -ENOTTY;
  3792. pci_read_config_byte(dev, pos + PCI_AF_CAP, &cap);
  3793. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  3794. return -ENOTTY;
  3795. if (probe)
  3796. return 0;
  3797. /*
  3798. * Wait for Transaction Pending bit to clear. A word-aligned test
  3799. * is used, so we use the conrol offset rather than status and shift
  3800. * the test bit to match.
  3801. */
  3802. if (!pci_wait_for_pending(dev, pos + PCI_AF_CTRL,
  3803. PCI_AF_STATUS_TP << 8))
  3804. pci_err(dev, "timed out waiting for pending transaction; performing AF function level reset anyway\n");
  3805. pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  3806. if (dev->imm_ready)
  3807. return 0;
  3808. /*
  3809. * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006,
  3810. * updated 27 July 2006; a device must complete an FLR within
  3811. * 100ms, but may silently discard requests while the FLR is in
  3812. * progress. Wait 100ms before trying to access the device.
  3813. */
  3814. msleep(100);
  3815. return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS);
  3816. }
  3817. /**
  3818. * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
  3819. * @dev: Device to reset.
  3820. * @probe: If set, only check if the device can be reset this way.
  3821. *
  3822. * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
  3823. * unset, it will be reinitialized internally when going from PCI_D3hot to
  3824. * PCI_D0. If that's the case and the device is not in a low-power state
  3825. * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
  3826. *
  3827. * NOTE: This causes the caller to sleep for twice the device power transition
  3828. * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
  3829. * by default (i.e. unless the @dev's d3_delay field has a different value).
  3830. * Moreover, only devices in D0 can be reset by this function.
  3831. */
  3832. static int pci_pm_reset(struct pci_dev *dev, int probe)
  3833. {
  3834. u16 csr;
  3835. if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET)
  3836. return -ENOTTY;
  3837. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &csr);
  3838. if (csr & PCI_PM_CTRL_NO_SOFT_RESET)
  3839. return -ENOTTY;
  3840. if (probe)
  3841. return 0;
  3842. if (dev->current_state != PCI_D0)
  3843. return -EINVAL;
  3844. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3845. csr |= PCI_D3hot;
  3846. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3847. pci_dev_d3_sleep(dev);
  3848. csr &= ~PCI_PM_CTRL_STATE_MASK;
  3849. csr |= PCI_D0;
  3850. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr);
  3851. pci_dev_d3_sleep(dev);
  3852. return pci_dev_wait(dev, "PM D3->D0", PCIE_RESET_READY_POLL_MS);
  3853. }
  3854. /**
  3855. * pcie_wait_for_link - Wait until link is active or inactive
  3856. * @pdev: Bridge device
  3857. * @active: waiting for active or inactive?
  3858. *
  3859. * Use this to wait till link becomes active or inactive.
  3860. */
  3861. bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
  3862. {
  3863. int timeout = 1000;
  3864. bool ret;
  3865. u16 lnk_status;
  3866. /*
  3867. * Some controllers might not implement link active reporting. In this
  3868. * case, we wait for 1000 + 100 ms.
  3869. */
  3870. if (!pdev->link_active_reporting) {
  3871. msleep(1100);
  3872. return true;
  3873. }
  3874. /*
  3875. * PCIe r4.0 sec 6.6.1, a component must enter LTSSM Detect within 20ms,
  3876. * after which we should expect an link active if the reset was
  3877. * successful. If so, software must wait a minimum 100ms before sending
  3878. * configuration requests to devices downstream this port.
  3879. *
  3880. * If the link fails to activate, either the device was physically
  3881. * removed or the link is permanently failed.
  3882. */
  3883. if (active)
  3884. msleep(20);
  3885. for (;;) {
  3886. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  3887. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  3888. if (ret == active)
  3889. break;
  3890. if (timeout <= 0)
  3891. break;
  3892. msleep(10);
  3893. timeout -= 10;
  3894. }
  3895. if (active && ret)
  3896. msleep(100);
  3897. else if (ret != active)
  3898. pci_info(pdev, "Data Link Layer Link Active not %s in 1000 msec\n",
  3899. active ? "set" : "cleared");
  3900. return ret == active;
  3901. }
  3902. void pci_reset_secondary_bus(struct pci_dev *dev)
  3903. {
  3904. u16 ctrl;
  3905. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &ctrl);
  3906. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  3907. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  3908. /*
  3909. * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
  3910. * this to 2ms to ensure that we meet the minimum requirement.
  3911. */
  3912. msleep(2);
  3913. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  3914. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
  3915. /*
  3916. * Trhfa for conventional PCI is 2^25 clock cycles.
  3917. * Assuming a minimum 33MHz clock this results in a 1s
  3918. * delay before we can consider subordinate devices to
  3919. * be re-initialized. PCIe has some ways to shorten this,
  3920. * but we don't make use of them yet.
  3921. */
  3922. ssleep(1);
  3923. }
  3924. void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
  3925. {
  3926. pci_reset_secondary_bus(dev);
  3927. }
  3928. /**
  3929. * pci_bridge_secondary_bus_reset - Reset the secondary bus on a PCI bridge.
  3930. * @dev: Bridge device
  3931. *
  3932. * Use the bridge control register to assert reset on the secondary bus.
  3933. * Devices on the secondary bus are left in power-on state.
  3934. */
  3935. int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
  3936. {
  3937. pcibios_reset_secondary_bus(dev);
  3938. return pci_dev_wait(dev, "bus reset", PCIE_RESET_READY_POLL_MS);
  3939. }
  3940. EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset);
  3941. static int pci_parent_bus_reset(struct pci_dev *dev, int probe)
  3942. {
  3943. struct pci_dev *pdev;
  3944. if (pci_is_root_bus(dev->bus) || dev->subordinate ||
  3945. !dev->bus->self || dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
  3946. return -ENOTTY;
  3947. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  3948. if (pdev != dev)
  3949. return -ENOTTY;
  3950. if (probe)
  3951. return 0;
  3952. return pci_bridge_secondary_bus_reset(dev->bus->self);
  3953. }
  3954. static int pci_reset_hotplug_slot(struct hotplug_slot *hotplug, int probe)
  3955. {
  3956. int rc = -ENOTTY;
  3957. if (!hotplug || !try_module_get(hotplug->owner))
  3958. return rc;
  3959. if (hotplug->ops->reset_slot)
  3960. rc = hotplug->ops->reset_slot(hotplug, probe);
  3961. module_put(hotplug->owner);
  3962. return rc;
  3963. }
  3964. static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe)
  3965. {
  3966. struct pci_dev *pdev;
  3967. if (dev->subordinate || !dev->slot ||
  3968. dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET)
  3969. return -ENOTTY;
  3970. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  3971. if (pdev != dev && pdev->slot == dev->slot)
  3972. return -ENOTTY;
  3973. return pci_reset_hotplug_slot(dev->slot->hotplug, probe);
  3974. }
  3975. static void pci_dev_lock(struct pci_dev *dev)
  3976. {
  3977. pci_cfg_access_lock(dev);
  3978. /* block PM suspend, driver probe, etc. */
  3979. device_lock(&dev->dev);
  3980. }
  3981. /* Return 1 on successful lock, 0 on contention */
  3982. static int pci_dev_trylock(struct pci_dev *dev)
  3983. {
  3984. if (pci_cfg_access_trylock(dev)) {
  3985. if (device_trylock(&dev->dev))
  3986. return 1;
  3987. pci_cfg_access_unlock(dev);
  3988. }
  3989. return 0;
  3990. }
  3991. static void pci_dev_unlock(struct pci_dev *dev)
  3992. {
  3993. device_unlock(&dev->dev);
  3994. pci_cfg_access_unlock(dev);
  3995. }
  3996. static void pci_dev_save_and_disable(struct pci_dev *dev)
  3997. {
  3998. const struct pci_error_handlers *err_handler =
  3999. dev->driver ? dev->driver->err_handler : NULL;
  4000. /*
  4001. * dev->driver->err_handler->reset_prepare() is protected against
  4002. * races with ->remove() by the device lock, which must be held by
  4003. * the caller.
  4004. */
  4005. if (err_handler && err_handler->reset_prepare)
  4006. err_handler->reset_prepare(dev);
  4007. /*
  4008. * Wake-up device prior to save. PM registers default to D0 after
  4009. * reset and a simple register restore doesn't reliably return
  4010. * to a non-D0 state anyway.
  4011. */
  4012. pci_set_power_state(dev, PCI_D0);
  4013. pci_save_state(dev);
  4014. /*
  4015. * Disable the device by clearing the Command register, except for
  4016. * INTx-disable which is set. This not only disables MMIO and I/O port
  4017. * BARs, but also prevents the device from being Bus Master, preventing
  4018. * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
  4019. * compliant devices, INTx-disable prevents legacy interrupts.
  4020. */
  4021. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  4022. }
  4023. static void pci_dev_restore(struct pci_dev *dev)
  4024. {
  4025. const struct pci_error_handlers *err_handler =
  4026. dev->driver ? dev->driver->err_handler : NULL;
  4027. pci_restore_state(dev);
  4028. /*
  4029. * dev->driver->err_handler->reset_done() is protected against
  4030. * races with ->remove() by the device lock, which must be held by
  4031. * the caller.
  4032. */
  4033. if (err_handler && err_handler->reset_done)
  4034. err_handler->reset_done(dev);
  4035. }
  4036. /**
  4037. * __pci_reset_function_locked - reset a PCI device function while holding
  4038. * the @dev mutex lock.
  4039. * @dev: PCI device to reset
  4040. *
  4041. * Some devices allow an individual function to be reset without affecting
  4042. * other functions in the same device. The PCI device must be responsive
  4043. * to PCI config space in order to use this function.
  4044. *
  4045. * The device function is presumed to be unused and the caller is holding
  4046. * the device mutex lock when this function is called.
  4047. * Resetting the device will make the contents of PCI configuration space
  4048. * random, so any caller of this must be prepared to reinitialise the
  4049. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  4050. * etc.
  4051. *
  4052. * Returns 0 if the device function was successfully reset or negative if the
  4053. * device doesn't support resetting a single function.
  4054. */
  4055. int __pci_reset_function_locked(struct pci_dev *dev)
  4056. {
  4057. int rc;
  4058. might_sleep();
  4059. /*
  4060. * A reset method returns -ENOTTY if it doesn't support this device
  4061. * and we should try the next method.
  4062. *
  4063. * If it returns 0 (success), we're finished. If it returns any
  4064. * other error, we're also finished: this indicates that further
  4065. * reset mechanisms might be broken on the device.
  4066. */
  4067. rc = pci_dev_specific_reset(dev, 0);
  4068. if (rc != -ENOTTY)
  4069. return rc;
  4070. if (pcie_has_flr(dev)) {
  4071. rc = pcie_flr(dev);
  4072. if (rc != -ENOTTY)
  4073. return rc;
  4074. }
  4075. rc = pci_af_flr(dev, 0);
  4076. if (rc != -ENOTTY)
  4077. return rc;
  4078. rc = pci_pm_reset(dev, 0);
  4079. if (rc != -ENOTTY)
  4080. return rc;
  4081. rc = pci_dev_reset_slot_function(dev, 0);
  4082. if (rc != -ENOTTY)
  4083. return rc;
  4084. return pci_parent_bus_reset(dev, 0);
  4085. }
  4086. EXPORT_SYMBOL_GPL(__pci_reset_function_locked);
  4087. /**
  4088. * pci_probe_reset_function - check whether the device can be safely reset
  4089. * @dev: PCI device to reset
  4090. *
  4091. * Some devices allow an individual function to be reset without affecting
  4092. * other functions in the same device. The PCI device must be responsive
  4093. * to PCI config space in order to use this function.
  4094. *
  4095. * Returns 0 if the device function can be reset or negative if the
  4096. * device doesn't support resetting a single function.
  4097. */
  4098. int pci_probe_reset_function(struct pci_dev *dev)
  4099. {
  4100. int rc;
  4101. might_sleep();
  4102. rc = pci_dev_specific_reset(dev, 1);
  4103. if (rc != -ENOTTY)
  4104. return rc;
  4105. if (pcie_has_flr(dev))
  4106. return 0;
  4107. rc = pci_af_flr(dev, 1);
  4108. if (rc != -ENOTTY)
  4109. return rc;
  4110. rc = pci_pm_reset(dev, 1);
  4111. if (rc != -ENOTTY)
  4112. return rc;
  4113. rc = pci_dev_reset_slot_function(dev, 1);
  4114. if (rc != -ENOTTY)
  4115. return rc;
  4116. return pci_parent_bus_reset(dev, 1);
  4117. }
  4118. /**
  4119. * pci_reset_function - quiesce and reset a PCI device function
  4120. * @dev: PCI device to reset
  4121. *
  4122. * Some devices allow an individual function to be reset without affecting
  4123. * other functions in the same device. The PCI device must be responsive
  4124. * to PCI config space in order to use this function.
  4125. *
  4126. * This function does not just reset the PCI portion of a device, but
  4127. * clears all the state associated with the device. This function differs
  4128. * from __pci_reset_function_locked() in that it saves and restores device state
  4129. * over the reset and takes the PCI device lock.
  4130. *
  4131. * Returns 0 if the device function was successfully reset or negative if the
  4132. * device doesn't support resetting a single function.
  4133. */
  4134. int pci_reset_function(struct pci_dev *dev)
  4135. {
  4136. int rc;
  4137. if (!dev->reset_fn)
  4138. return -ENOTTY;
  4139. pci_dev_lock(dev);
  4140. pci_dev_save_and_disable(dev);
  4141. rc = __pci_reset_function_locked(dev);
  4142. pci_dev_restore(dev);
  4143. pci_dev_unlock(dev);
  4144. return rc;
  4145. }
  4146. EXPORT_SYMBOL_GPL(pci_reset_function);
  4147. /**
  4148. * pci_reset_function_locked - quiesce and reset a PCI device function
  4149. * @dev: PCI device to reset
  4150. *
  4151. * Some devices allow an individual function to be reset without affecting
  4152. * other functions in the same device. The PCI device must be responsive
  4153. * to PCI config space in order to use this function.
  4154. *
  4155. * This function does not just reset the PCI portion of a device, but
  4156. * clears all the state associated with the device. This function differs
  4157. * from __pci_reset_function_locked() in that it saves and restores device state
  4158. * over the reset. It also differs from pci_reset_function() in that it
  4159. * requires the PCI device lock to be held.
  4160. *
  4161. * Returns 0 if the device function was successfully reset or negative if the
  4162. * device doesn't support resetting a single function.
  4163. */
  4164. int pci_reset_function_locked(struct pci_dev *dev)
  4165. {
  4166. int rc;
  4167. if (!dev->reset_fn)
  4168. return -ENOTTY;
  4169. pci_dev_save_and_disable(dev);
  4170. rc = __pci_reset_function_locked(dev);
  4171. pci_dev_restore(dev);
  4172. return rc;
  4173. }
  4174. EXPORT_SYMBOL_GPL(pci_reset_function_locked);
  4175. /**
  4176. * pci_try_reset_function - quiesce and reset a PCI device function
  4177. * @dev: PCI device to reset
  4178. *
  4179. * Same as above, except return -EAGAIN if unable to lock device.
  4180. */
  4181. int pci_try_reset_function(struct pci_dev *dev)
  4182. {
  4183. int rc;
  4184. if (!dev->reset_fn)
  4185. return -ENOTTY;
  4186. if (!pci_dev_trylock(dev))
  4187. return -EAGAIN;
  4188. pci_dev_save_and_disable(dev);
  4189. rc = __pci_reset_function_locked(dev);
  4190. pci_dev_restore(dev);
  4191. pci_dev_unlock(dev);
  4192. return rc;
  4193. }
  4194. EXPORT_SYMBOL_GPL(pci_try_reset_function);
  4195. /* Do any devices on or below this bus prevent a bus reset? */
  4196. static bool pci_bus_resetable(struct pci_bus *bus)
  4197. {
  4198. struct pci_dev *dev;
  4199. if (bus->self && (bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
  4200. return false;
  4201. list_for_each_entry(dev, &bus->devices, bus_list) {
  4202. if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
  4203. (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
  4204. return false;
  4205. }
  4206. return true;
  4207. }
  4208. /* Lock devices from the top of the tree down */
  4209. static void pci_bus_lock(struct pci_bus *bus)
  4210. {
  4211. struct pci_dev *dev;
  4212. list_for_each_entry(dev, &bus->devices, bus_list) {
  4213. pci_dev_lock(dev);
  4214. if (dev->subordinate)
  4215. pci_bus_lock(dev->subordinate);
  4216. }
  4217. }
  4218. /* Unlock devices from the bottom of the tree up */
  4219. static void pci_bus_unlock(struct pci_bus *bus)
  4220. {
  4221. struct pci_dev *dev;
  4222. list_for_each_entry(dev, &bus->devices, bus_list) {
  4223. if (dev->subordinate)
  4224. pci_bus_unlock(dev->subordinate);
  4225. pci_dev_unlock(dev);
  4226. }
  4227. }
  4228. /* Return 1 on successful lock, 0 on contention */
  4229. static int pci_bus_trylock(struct pci_bus *bus)
  4230. {
  4231. struct pci_dev *dev;
  4232. list_for_each_entry(dev, &bus->devices, bus_list) {
  4233. if (!pci_dev_trylock(dev))
  4234. goto unlock;
  4235. if (dev->subordinate) {
  4236. if (!pci_bus_trylock(dev->subordinate)) {
  4237. pci_dev_unlock(dev);
  4238. goto unlock;
  4239. }
  4240. }
  4241. }
  4242. return 1;
  4243. unlock:
  4244. list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
  4245. if (dev->subordinate)
  4246. pci_bus_unlock(dev->subordinate);
  4247. pci_dev_unlock(dev);
  4248. }
  4249. return 0;
  4250. }
  4251. /* Do any devices on or below this slot prevent a bus reset? */
  4252. static bool pci_slot_resetable(struct pci_slot *slot)
  4253. {
  4254. struct pci_dev *dev;
  4255. if (slot->bus->self &&
  4256. (slot->bus->self->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET))
  4257. return false;
  4258. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4259. if (!dev->slot || dev->slot != slot)
  4260. continue;
  4261. if (dev->dev_flags & PCI_DEV_FLAGS_NO_BUS_RESET ||
  4262. (dev->subordinate && !pci_bus_resetable(dev->subordinate)))
  4263. return false;
  4264. }
  4265. return true;
  4266. }
  4267. /* Lock devices from the top of the tree down */
  4268. static void pci_slot_lock(struct pci_slot *slot)
  4269. {
  4270. struct pci_dev *dev;
  4271. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4272. if (!dev->slot || dev->slot != slot)
  4273. continue;
  4274. pci_dev_lock(dev);
  4275. if (dev->subordinate)
  4276. pci_bus_lock(dev->subordinate);
  4277. }
  4278. }
  4279. /* Unlock devices from the bottom of the tree up */
  4280. static void pci_slot_unlock(struct pci_slot *slot)
  4281. {
  4282. struct pci_dev *dev;
  4283. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4284. if (!dev->slot || dev->slot != slot)
  4285. continue;
  4286. if (dev->subordinate)
  4287. pci_bus_unlock(dev->subordinate);
  4288. pci_dev_unlock(dev);
  4289. }
  4290. }
  4291. /* Return 1 on successful lock, 0 on contention */
  4292. static int pci_slot_trylock(struct pci_slot *slot)
  4293. {
  4294. struct pci_dev *dev;
  4295. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4296. if (!dev->slot || dev->slot != slot)
  4297. continue;
  4298. if (!pci_dev_trylock(dev))
  4299. goto unlock;
  4300. if (dev->subordinate) {
  4301. if (!pci_bus_trylock(dev->subordinate)) {
  4302. pci_dev_unlock(dev);
  4303. goto unlock;
  4304. }
  4305. }
  4306. }
  4307. return 1;
  4308. unlock:
  4309. list_for_each_entry_continue_reverse(dev,
  4310. &slot->bus->devices, bus_list) {
  4311. if (!dev->slot || dev->slot != slot)
  4312. continue;
  4313. if (dev->subordinate)
  4314. pci_bus_unlock(dev->subordinate);
  4315. pci_dev_unlock(dev);
  4316. }
  4317. return 0;
  4318. }
  4319. /* Save and disable devices from the top of the tree down */
  4320. static void pci_bus_save_and_disable(struct pci_bus *bus)
  4321. {
  4322. struct pci_dev *dev;
  4323. list_for_each_entry(dev, &bus->devices, bus_list) {
  4324. pci_dev_lock(dev);
  4325. pci_dev_save_and_disable(dev);
  4326. pci_dev_unlock(dev);
  4327. if (dev->subordinate)
  4328. pci_bus_save_and_disable(dev->subordinate);
  4329. }
  4330. }
  4331. /*
  4332. * Restore devices from top of the tree down - parent bridges need to be
  4333. * restored before we can get to subordinate devices.
  4334. */
  4335. static void pci_bus_restore(struct pci_bus *bus)
  4336. {
  4337. struct pci_dev *dev;
  4338. list_for_each_entry(dev, &bus->devices, bus_list) {
  4339. pci_dev_lock(dev);
  4340. pci_dev_restore(dev);
  4341. pci_dev_unlock(dev);
  4342. if (dev->subordinate)
  4343. pci_bus_restore(dev->subordinate);
  4344. }
  4345. }
  4346. /* Save and disable devices from the top of the tree down */
  4347. static void pci_slot_save_and_disable(struct pci_slot *slot)
  4348. {
  4349. struct pci_dev *dev;
  4350. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4351. if (!dev->slot || dev->slot != slot)
  4352. continue;
  4353. pci_dev_save_and_disable(dev);
  4354. if (dev->subordinate)
  4355. pci_bus_save_and_disable(dev->subordinate);
  4356. }
  4357. }
  4358. /*
  4359. * Restore devices from top of the tree down - parent bridges need to be
  4360. * restored before we can get to subordinate devices.
  4361. */
  4362. static void pci_slot_restore(struct pci_slot *slot)
  4363. {
  4364. struct pci_dev *dev;
  4365. list_for_each_entry(dev, &slot->bus->devices, bus_list) {
  4366. if (!dev->slot || dev->slot != slot)
  4367. continue;
  4368. pci_dev_lock(dev);
  4369. pci_dev_restore(dev);
  4370. pci_dev_unlock(dev);
  4371. if (dev->subordinate)
  4372. pci_bus_restore(dev->subordinate);
  4373. }
  4374. }
  4375. static int pci_slot_reset(struct pci_slot *slot, int probe)
  4376. {
  4377. int rc;
  4378. if (!slot || !pci_slot_resetable(slot))
  4379. return -ENOTTY;
  4380. if (!probe)
  4381. pci_slot_lock(slot);
  4382. might_sleep();
  4383. rc = pci_reset_hotplug_slot(slot->hotplug, probe);
  4384. if (!probe)
  4385. pci_slot_unlock(slot);
  4386. return rc;
  4387. }
  4388. /**
  4389. * pci_probe_reset_slot - probe whether a PCI slot can be reset
  4390. * @slot: PCI slot to probe
  4391. *
  4392. * Return 0 if slot can be reset, negative if a slot reset is not supported.
  4393. */
  4394. int pci_probe_reset_slot(struct pci_slot *slot)
  4395. {
  4396. return pci_slot_reset(slot, 1);
  4397. }
  4398. EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
  4399. /**
  4400. * __pci_reset_slot - Try to reset a PCI slot
  4401. * @slot: PCI slot to reset
  4402. *
  4403. * A PCI bus may host multiple slots, each slot may support a reset mechanism
  4404. * independent of other slots. For instance, some slots may support slot power
  4405. * control. In the case of a 1:1 bus to slot architecture, this function may
  4406. * wrap the bus reset to avoid spurious slot related events such as hotplug.
  4407. * Generally a slot reset should be attempted before a bus reset. All of the
  4408. * function of the slot and any subordinate buses behind the slot are reset
  4409. * through this function. PCI config space of all devices in the slot and
  4410. * behind the slot is saved before and restored after reset.
  4411. *
  4412. * Same as above except return -EAGAIN if the slot cannot be locked
  4413. */
  4414. static int __pci_reset_slot(struct pci_slot *slot)
  4415. {
  4416. int rc;
  4417. rc = pci_slot_reset(slot, 1);
  4418. if (rc)
  4419. return rc;
  4420. pci_slot_save_and_disable(slot);
  4421. if (pci_slot_trylock(slot)) {
  4422. might_sleep();
  4423. rc = pci_reset_hotplug_slot(slot->hotplug, 0);
  4424. pci_slot_unlock(slot);
  4425. } else
  4426. rc = -EAGAIN;
  4427. pci_slot_restore(slot);
  4428. return rc;
  4429. }
  4430. static int pci_bus_reset(struct pci_bus *bus, int probe)
  4431. {
  4432. int ret;
  4433. if (!bus->self || !pci_bus_resetable(bus))
  4434. return -ENOTTY;
  4435. if (probe)
  4436. return 0;
  4437. pci_bus_lock(bus);
  4438. might_sleep();
  4439. ret = pci_bridge_secondary_bus_reset(bus->self);
  4440. pci_bus_unlock(bus);
  4441. return ret;
  4442. }
  4443. /**
  4444. * pci_bus_error_reset - reset the bridge's subordinate bus
  4445. * @bridge: The parent device that connects to the bus to reset
  4446. *
  4447. * This function will first try to reset the slots on this bus if the method is
  4448. * available. If slot reset fails or is not available, this will fall back to a
  4449. * secondary bus reset.
  4450. */
  4451. int pci_bus_error_reset(struct pci_dev *bridge)
  4452. {
  4453. struct pci_bus *bus = bridge->subordinate;
  4454. struct pci_slot *slot;
  4455. if (!bus)
  4456. return -ENOTTY;
  4457. mutex_lock(&pci_slot_mutex);
  4458. if (list_empty(&bus->slots))
  4459. goto bus_reset;
  4460. list_for_each_entry(slot, &bus->slots, list)
  4461. if (pci_probe_reset_slot(slot))
  4462. goto bus_reset;
  4463. list_for_each_entry(slot, &bus->slots, list)
  4464. if (pci_slot_reset(slot, 0))
  4465. goto bus_reset;
  4466. mutex_unlock(&pci_slot_mutex);
  4467. return 0;
  4468. bus_reset:
  4469. mutex_unlock(&pci_slot_mutex);
  4470. return pci_bus_reset(bridge->subordinate, 0);
  4471. }
  4472. /**
  4473. * pci_probe_reset_bus - probe whether a PCI bus can be reset
  4474. * @bus: PCI bus to probe
  4475. *
  4476. * Return 0 if bus can be reset, negative if a bus reset is not supported.
  4477. */
  4478. int pci_probe_reset_bus(struct pci_bus *bus)
  4479. {
  4480. return pci_bus_reset(bus, 1);
  4481. }
  4482. EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
  4483. /**
  4484. * __pci_reset_bus - Try to reset a PCI bus
  4485. * @bus: top level PCI bus to reset
  4486. *
  4487. * Same as above except return -EAGAIN if the bus cannot be locked
  4488. */
  4489. static int __pci_reset_bus(struct pci_bus *bus)
  4490. {
  4491. int rc;
  4492. rc = pci_bus_reset(bus, 1);
  4493. if (rc)
  4494. return rc;
  4495. pci_bus_save_and_disable(bus);
  4496. if (pci_bus_trylock(bus)) {
  4497. might_sleep();
  4498. rc = pci_bridge_secondary_bus_reset(bus->self);
  4499. pci_bus_unlock(bus);
  4500. } else
  4501. rc = -EAGAIN;
  4502. pci_bus_restore(bus);
  4503. return rc;
  4504. }
  4505. /**
  4506. * pci_reset_bus - Try to reset a PCI bus
  4507. * @pdev: top level PCI device to reset via slot/bus
  4508. *
  4509. * Same as above except return -EAGAIN if the bus cannot be locked
  4510. */
  4511. int pci_reset_bus(struct pci_dev *pdev)
  4512. {
  4513. return (!pci_probe_reset_slot(pdev->slot)) ?
  4514. __pci_reset_slot(pdev->slot) : __pci_reset_bus(pdev->bus);
  4515. }
  4516. EXPORT_SYMBOL_GPL(pci_reset_bus);
  4517. /**
  4518. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  4519. * @dev: PCI device to query
  4520. *
  4521. * Returns mmrbc: maximum designed memory read count in bytes
  4522. * or appropriate error value.
  4523. */
  4524. int pcix_get_max_mmrbc(struct pci_dev *dev)
  4525. {
  4526. int cap;
  4527. u32 stat;
  4528. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4529. if (!cap)
  4530. return -EINVAL;
  4531. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4532. return -EINVAL;
  4533. return 512 << ((stat & PCI_X_STATUS_MAX_READ) >> 21);
  4534. }
  4535. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  4536. /**
  4537. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  4538. * @dev: PCI device to query
  4539. *
  4540. * Returns mmrbc: maximum memory read count in bytes
  4541. * or appropriate error value.
  4542. */
  4543. int pcix_get_mmrbc(struct pci_dev *dev)
  4544. {
  4545. int cap;
  4546. u16 cmd;
  4547. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4548. if (!cap)
  4549. return -EINVAL;
  4550. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4551. return -EINVAL;
  4552. return 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  4553. }
  4554. EXPORT_SYMBOL(pcix_get_mmrbc);
  4555. /**
  4556. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  4557. * @dev: PCI device to query
  4558. * @mmrbc: maximum memory read count in bytes
  4559. * valid values are 512, 1024, 2048, 4096
  4560. *
  4561. * If possible sets maximum memory read byte count, some bridges have erratas
  4562. * that prevent this.
  4563. */
  4564. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  4565. {
  4566. int cap;
  4567. u32 stat, v, o;
  4568. u16 cmd;
  4569. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  4570. return -EINVAL;
  4571. v = ffs(mmrbc) - 10;
  4572. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  4573. if (!cap)
  4574. return -EINVAL;
  4575. if (pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat))
  4576. return -EINVAL;
  4577. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  4578. return -E2BIG;
  4579. if (pci_read_config_word(dev, cap + PCI_X_CMD, &cmd))
  4580. return -EINVAL;
  4581. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  4582. if (o != v) {
  4583. if (v > o && (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  4584. return -EIO;
  4585. cmd &= ~PCI_X_CMD_MAX_READ;
  4586. cmd |= v << 2;
  4587. if (pci_write_config_word(dev, cap + PCI_X_CMD, cmd))
  4588. return -EIO;
  4589. }
  4590. return 0;
  4591. }
  4592. EXPORT_SYMBOL(pcix_set_mmrbc);
  4593. /**
  4594. * pcie_get_readrq - get PCI Express read request size
  4595. * @dev: PCI device to query
  4596. *
  4597. * Returns maximum memory read request in bytes
  4598. * or appropriate error value.
  4599. */
  4600. int pcie_get_readrq(struct pci_dev *dev)
  4601. {
  4602. u16 ctl;
  4603. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4604. return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  4605. }
  4606. EXPORT_SYMBOL(pcie_get_readrq);
  4607. /**
  4608. * pcie_set_readrq - set PCI Express maximum memory read request
  4609. * @dev: PCI device to query
  4610. * @rq: maximum memory read count in bytes
  4611. * valid values are 128, 256, 512, 1024, 2048, 4096
  4612. *
  4613. * If possible sets maximum memory read request in bytes
  4614. */
  4615. int pcie_set_readrq(struct pci_dev *dev, int rq)
  4616. {
  4617. u16 v;
  4618. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  4619. return -EINVAL;
  4620. /*
  4621. * If using the "performance" PCIe config, we clamp the
  4622. * read rq size to the max packet size to prevent the
  4623. * host bridge generating requests larger than we can
  4624. * cope with
  4625. */
  4626. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  4627. int mps = pcie_get_mps(dev);
  4628. if (mps < rq)
  4629. rq = mps;
  4630. }
  4631. v = (ffs(rq) - 8) << 12;
  4632. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4633. PCI_EXP_DEVCTL_READRQ, v);
  4634. }
  4635. EXPORT_SYMBOL(pcie_set_readrq);
  4636. /**
  4637. * pcie_get_mps - get PCI Express maximum payload size
  4638. * @dev: PCI device to query
  4639. *
  4640. * Returns maximum payload size in bytes
  4641. */
  4642. int pcie_get_mps(struct pci_dev *dev)
  4643. {
  4644. u16 ctl;
  4645. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  4646. return 128 << ((ctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
  4647. }
  4648. EXPORT_SYMBOL(pcie_get_mps);
  4649. /**
  4650. * pcie_set_mps - set PCI Express maximum payload size
  4651. * @dev: PCI device to query
  4652. * @mps: maximum payload size in bytes
  4653. * valid values are 128, 256, 512, 1024, 2048, 4096
  4654. *
  4655. * If possible sets maximum payload size
  4656. */
  4657. int pcie_set_mps(struct pci_dev *dev, int mps)
  4658. {
  4659. u16 v;
  4660. if (mps < 128 || mps > 4096 || !is_power_of_2(mps))
  4661. return -EINVAL;
  4662. v = ffs(mps) - 8;
  4663. if (v > dev->pcie_mpss)
  4664. return -EINVAL;
  4665. v <<= 5;
  4666. return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  4667. PCI_EXP_DEVCTL_PAYLOAD, v);
  4668. }
  4669. EXPORT_SYMBOL(pcie_set_mps);
  4670. /**
  4671. * pcie_bandwidth_available - determine minimum link settings of a PCIe
  4672. * device and its bandwidth limitation
  4673. * @dev: PCI device to query
  4674. * @limiting_dev: storage for device causing the bandwidth limitation
  4675. * @speed: storage for speed of limiting device
  4676. * @width: storage for width of limiting device
  4677. *
  4678. * Walk up the PCI device chain and find the point where the minimum
  4679. * bandwidth is available. Return the bandwidth available there and (if
  4680. * limiting_dev, speed, and width pointers are supplied) information about
  4681. * that point. The bandwidth returned is in Mb/s, i.e., megabits/second of
  4682. * raw bandwidth.
  4683. */
  4684. u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,
  4685. enum pci_bus_speed *speed,
  4686. enum pcie_link_width *width)
  4687. {
  4688. u16 lnksta;
  4689. enum pci_bus_speed next_speed;
  4690. enum pcie_link_width next_width;
  4691. u32 bw, next_bw;
  4692. if (speed)
  4693. *speed = PCI_SPEED_UNKNOWN;
  4694. if (width)
  4695. *width = PCIE_LNK_WIDTH_UNKNOWN;
  4696. bw = 0;
  4697. while (dev) {
  4698. pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
  4699. next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
  4700. next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
  4701. PCI_EXP_LNKSTA_NLW_SHIFT;
  4702. next_bw = next_width * PCIE_SPEED2MBS_ENC(next_speed);
  4703. /* Check if current device limits the total bandwidth */
  4704. if (!bw || next_bw <= bw) {
  4705. bw = next_bw;
  4706. if (limiting_dev)
  4707. *limiting_dev = dev;
  4708. if (speed)
  4709. *speed = next_speed;
  4710. if (width)
  4711. *width = next_width;
  4712. }
  4713. dev = pci_upstream_bridge(dev);
  4714. }
  4715. return bw;
  4716. }
  4717. EXPORT_SYMBOL(pcie_bandwidth_available);
  4718. /**
  4719. * pcie_get_speed_cap - query for the PCI device's link speed capability
  4720. * @dev: PCI device to query
  4721. *
  4722. * Query the PCI device speed capability. Return the maximum link speed
  4723. * supported by the device.
  4724. */
  4725. enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
  4726. {
  4727. u32 lnkcap2, lnkcap;
  4728. /*
  4729. * Link Capabilities 2 was added in PCIe r3.0, sec 7.8.18. The
  4730. * implementation note there recommends using the Supported Link
  4731. * Speeds Vector in Link Capabilities 2 when supported.
  4732. *
  4733. * Without Link Capabilities 2, i.e., prior to PCIe r3.0, software
  4734. * should use the Supported Link Speeds field in Link Capabilities,
  4735. * where only 2.5 GT/s and 5.0 GT/s speeds were defined.
  4736. */
  4737. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP2, &lnkcap2);
  4738. if (lnkcap2) { /* PCIe r3.0-compliant */
  4739. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
  4740. return PCIE_SPEED_16_0GT;
  4741. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  4742. return PCIE_SPEED_8_0GT;
  4743. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  4744. return PCIE_SPEED_5_0GT;
  4745. else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  4746. return PCIE_SPEED_2_5GT;
  4747. return PCI_SPEED_UNKNOWN;
  4748. }
  4749. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
  4750. if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_5_0GB)
  4751. return PCIE_SPEED_5_0GT;
  4752. else if ((lnkcap & PCI_EXP_LNKCAP_SLS) == PCI_EXP_LNKCAP_SLS_2_5GB)
  4753. return PCIE_SPEED_2_5GT;
  4754. return PCI_SPEED_UNKNOWN;
  4755. }
  4756. EXPORT_SYMBOL(pcie_get_speed_cap);
  4757. /**
  4758. * pcie_get_width_cap - query for the PCI device's link width capability
  4759. * @dev: PCI device to query
  4760. *
  4761. * Query the PCI device width capability. Return the maximum link width
  4762. * supported by the device.
  4763. */
  4764. enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
  4765. {
  4766. u32 lnkcap;
  4767. pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
  4768. if (lnkcap)
  4769. return (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
  4770. return PCIE_LNK_WIDTH_UNKNOWN;
  4771. }
  4772. EXPORT_SYMBOL(pcie_get_width_cap);
  4773. /**
  4774. * pcie_bandwidth_capable - calculate a PCI device's link bandwidth capability
  4775. * @dev: PCI device
  4776. * @speed: storage for link speed
  4777. * @width: storage for link width
  4778. *
  4779. * Calculate a PCI device's link bandwidth by querying for its link speed
  4780. * and width, multiplying them, and applying encoding overhead. The result
  4781. * is in Mb/s, i.e., megabits/second of raw bandwidth.
  4782. */
  4783. u32 pcie_bandwidth_capable(struct pci_dev *dev, enum pci_bus_speed *speed,
  4784. enum pcie_link_width *width)
  4785. {
  4786. *speed = pcie_get_speed_cap(dev);
  4787. *width = pcie_get_width_cap(dev);
  4788. if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
  4789. return 0;
  4790. return *width * PCIE_SPEED2MBS_ENC(*speed);
  4791. }
  4792. /**
  4793. * __pcie_print_link_status - Report the PCI device's link speed and width
  4794. * @dev: PCI device to query
  4795. * @verbose: Print info even when enough bandwidth is available
  4796. *
  4797. * If the available bandwidth at the device is less than the device is
  4798. * capable of, report the device's maximum possible bandwidth and the
  4799. * upstream link that limits its performance. If @verbose, always print
  4800. * the available bandwidth, even if the device isn't constrained.
  4801. */
  4802. void __pcie_print_link_status(struct pci_dev *dev, bool verbose)
  4803. {
  4804. enum pcie_link_width width, width_cap;
  4805. enum pci_bus_speed speed, speed_cap;
  4806. struct pci_dev *limiting_dev = NULL;
  4807. u32 bw_avail, bw_cap;
  4808. bw_cap = pcie_bandwidth_capable(dev, &speed_cap, &width_cap);
  4809. bw_avail = pcie_bandwidth_available(dev, &limiting_dev, &speed, &width);
  4810. if (bw_avail >= bw_cap && verbose)
  4811. pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth (%s x%d link)\n",
  4812. bw_cap / 1000, bw_cap % 1000,
  4813. PCIE_SPEED2STR(speed_cap), width_cap);
  4814. else if (bw_avail < bw_cap)
  4815. pci_info(dev, "%u.%03u Gb/s available PCIe bandwidth, limited by %s x%d link at %s (capable of %u.%03u Gb/s with %s x%d link)\n",
  4816. bw_avail / 1000, bw_avail % 1000,
  4817. PCIE_SPEED2STR(speed), width,
  4818. limiting_dev ? pci_name(limiting_dev) : "<unknown>",
  4819. bw_cap / 1000, bw_cap % 1000,
  4820. PCIE_SPEED2STR(speed_cap), width_cap);
  4821. }
  4822. /**
  4823. * pcie_print_link_status - Report the PCI device's link speed and width
  4824. * @dev: PCI device to query
  4825. *
  4826. * Report the available bandwidth at the device.
  4827. */
  4828. void pcie_print_link_status(struct pci_dev *dev)
  4829. {
  4830. __pcie_print_link_status(dev, true);
  4831. }
  4832. EXPORT_SYMBOL(pcie_print_link_status);
  4833. /**
  4834. * pci_select_bars - Make BAR mask from the type of resource
  4835. * @dev: the PCI device for which BAR mask is made
  4836. * @flags: resource type mask to be selected
  4837. *
  4838. * This helper routine makes bar mask from the type of resource.
  4839. */
  4840. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  4841. {
  4842. int i, bars = 0;
  4843. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  4844. if (pci_resource_flags(dev, i) & flags)
  4845. bars |= (1 << i);
  4846. return bars;
  4847. }
  4848. EXPORT_SYMBOL(pci_select_bars);
  4849. /* Some architectures require additional programming to enable VGA */
  4850. static arch_set_vga_state_t arch_set_vga_state;
  4851. void __init pci_register_set_vga_state(arch_set_vga_state_t func)
  4852. {
  4853. arch_set_vga_state = func; /* NULL disables */
  4854. }
  4855. static int pci_set_vga_state_arch(struct pci_dev *dev, bool decode,
  4856. unsigned int command_bits, u32 flags)
  4857. {
  4858. if (arch_set_vga_state)
  4859. return arch_set_vga_state(dev, decode, command_bits,
  4860. flags);
  4861. return 0;
  4862. }
  4863. /**
  4864. * pci_set_vga_state - set VGA decode state on device and parents if requested
  4865. * @dev: the PCI device
  4866. * @decode: true = enable decoding, false = disable decoding
  4867. * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
  4868. * @flags: traverse ancestors and change bridges
  4869. * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
  4870. */
  4871. int pci_set_vga_state(struct pci_dev *dev, bool decode,
  4872. unsigned int command_bits, u32 flags)
  4873. {
  4874. struct pci_bus *bus;
  4875. struct pci_dev *bridge;
  4876. u16 cmd;
  4877. int rc;
  4878. WARN_ON((flags & PCI_VGA_STATE_CHANGE_DECODES) && (command_bits & ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)));
  4879. /* ARCH specific VGA enables */
  4880. rc = pci_set_vga_state_arch(dev, decode, command_bits, flags);
  4881. if (rc)
  4882. return rc;
  4883. if (flags & PCI_VGA_STATE_CHANGE_DECODES) {
  4884. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  4885. if (decode == true)
  4886. cmd |= command_bits;
  4887. else
  4888. cmd &= ~command_bits;
  4889. pci_write_config_word(dev, PCI_COMMAND, cmd);
  4890. }
  4891. if (!(flags & PCI_VGA_STATE_CHANGE_BRIDGE))
  4892. return 0;
  4893. bus = dev->bus;
  4894. while (bus) {
  4895. bridge = bus->self;
  4896. if (bridge) {
  4897. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
  4898. &cmd);
  4899. if (decode == true)
  4900. cmd |= PCI_BRIDGE_CTL_VGA;
  4901. else
  4902. cmd &= ~PCI_BRIDGE_CTL_VGA;
  4903. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL,
  4904. cmd);
  4905. }
  4906. bus = bus->parent;
  4907. }
  4908. return 0;
  4909. }
  4910. /**
  4911. * pci_add_dma_alias - Add a DMA devfn alias for a device
  4912. * @dev: the PCI device for which alias is added
  4913. * @devfn: alias slot and function
  4914. *
  4915. * This helper encodes an 8-bit devfn as a bit number in dma_alias_mask
  4916. * which is used to program permissible bus-devfn source addresses for DMA
  4917. * requests in an IOMMU. These aliases factor into IOMMU group creation
  4918. * and are useful for devices generating DMA requests beyond or different
  4919. * from their logical bus-devfn. Examples include device quirks where the
  4920. * device simply uses the wrong devfn, as well as non-transparent bridges
  4921. * where the alias may be a proxy for devices in another domain.
  4922. *
  4923. * IOMMU group creation is performed during device discovery or addition,
  4924. * prior to any potential DMA mapping and therefore prior to driver probing
  4925. * (especially for userspace assigned devices where IOMMU group definition
  4926. * cannot be left as a userspace activity). DMA aliases should therefore
  4927. * be configured via quirks, such as the PCI fixup header quirk.
  4928. */
  4929. void pci_add_dma_alias(struct pci_dev *dev, u8 devfn)
  4930. {
  4931. if (!dev->dma_alias_mask)
  4932. dev->dma_alias_mask = bitmap_zalloc(U8_MAX, GFP_KERNEL);
  4933. if (!dev->dma_alias_mask) {
  4934. pci_warn(dev, "Unable to allocate DMA alias mask\n");
  4935. return;
  4936. }
  4937. set_bit(devfn, dev->dma_alias_mask);
  4938. pci_info(dev, "Enabling fixed DMA alias to %02x.%d\n",
  4939. PCI_SLOT(devfn), PCI_FUNC(devfn));
  4940. }
  4941. bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)
  4942. {
  4943. return (dev1->dma_alias_mask &&
  4944. test_bit(dev2->devfn, dev1->dma_alias_mask)) ||
  4945. (dev2->dma_alias_mask &&
  4946. test_bit(dev1->devfn, dev2->dma_alias_mask));
  4947. }
  4948. bool pci_device_is_present(struct pci_dev *pdev)
  4949. {
  4950. u32 v;
  4951. if (pci_dev_is_disconnected(pdev))
  4952. return false;
  4953. return pci_bus_read_dev_vendor_id(pdev->bus, pdev->devfn, &v, 0);
  4954. }
  4955. EXPORT_SYMBOL_GPL(pci_device_is_present);
  4956. void pci_ignore_hotplug(struct pci_dev *dev)
  4957. {
  4958. struct pci_dev *bridge = dev->bus->self;
  4959. dev->ignore_hotplug = 1;
  4960. /* Propagate the "ignore hotplug" setting to the parent bridge. */
  4961. if (bridge)
  4962. bridge->ignore_hotplug = 1;
  4963. }
  4964. EXPORT_SYMBOL_GPL(pci_ignore_hotplug);
  4965. resource_size_t __weak pcibios_default_alignment(void)
  4966. {
  4967. return 0;
  4968. }
  4969. #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
  4970. static char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
  4971. static DEFINE_SPINLOCK(resource_alignment_lock);
  4972. /**
  4973. * pci_specified_resource_alignment - get resource alignment specified by user.
  4974. * @dev: the PCI device to get
  4975. * @resize: whether or not to change resources' size when reassigning alignment
  4976. *
  4977. * RETURNS: Resource alignment if it is specified.
  4978. * Zero if it is not specified.
  4979. */
  4980. static resource_size_t pci_specified_resource_alignment(struct pci_dev *dev,
  4981. bool *resize)
  4982. {
  4983. int align_order, count;
  4984. resource_size_t align = pcibios_default_alignment();
  4985. const char *p;
  4986. int ret;
  4987. spin_lock(&resource_alignment_lock);
  4988. p = resource_alignment_param;
  4989. if (!*p && !align)
  4990. goto out;
  4991. if (pci_has_flag(PCI_PROBE_ONLY)) {
  4992. align = 0;
  4993. pr_info_once("PCI: Ignoring requested alignments (PCI_PROBE_ONLY)\n");
  4994. goto out;
  4995. }
  4996. while (*p) {
  4997. count = 0;
  4998. if (sscanf(p, "%d%n", &align_order, &count) == 1 &&
  4999. p[count] == '@') {
  5000. p += count + 1;
  5001. } else {
  5002. align_order = -1;
  5003. }
  5004. ret = pci_dev_str_match(dev, p, &p);
  5005. if (ret == 1) {
  5006. *resize = true;
  5007. if (align_order == -1)
  5008. align = PAGE_SIZE;
  5009. else
  5010. align = 1 << align_order;
  5011. break;
  5012. } else if (ret < 0) {
  5013. pr_err("PCI: Can't parse resource_alignment parameter: %s\n",
  5014. p);
  5015. break;
  5016. }
  5017. if (*p != ';' && *p != ',') {
  5018. /* End of param or invalid format */
  5019. break;
  5020. }
  5021. p++;
  5022. }
  5023. out:
  5024. spin_unlock(&resource_alignment_lock);
  5025. return align;
  5026. }
  5027. static void pci_request_resource_alignment(struct pci_dev *dev, int bar,
  5028. resource_size_t align, bool resize)
  5029. {
  5030. struct resource *r = &dev->resource[bar];
  5031. resource_size_t size;
  5032. if (!(r->flags & IORESOURCE_MEM))
  5033. return;
  5034. if (r->flags & IORESOURCE_PCI_FIXED) {
  5035. pci_info(dev, "BAR%d %pR: ignoring requested alignment %#llx\n",
  5036. bar, r, (unsigned long long)align);
  5037. return;
  5038. }
  5039. size = resource_size(r);
  5040. if (size >= align)
  5041. return;
  5042. /*
  5043. * Increase the alignment of the resource. There are two ways we
  5044. * can do this:
  5045. *
  5046. * 1) Increase the size of the resource. BARs are aligned on their
  5047. * size, so when we reallocate space for this resource, we'll
  5048. * allocate it with the larger alignment. This also prevents
  5049. * assignment of any other BARs inside the alignment region, so
  5050. * if we're requesting page alignment, this means no other BARs
  5051. * will share the page.
  5052. *
  5053. * The disadvantage is that this makes the resource larger than
  5054. * the hardware BAR, which may break drivers that compute things
  5055. * based on the resource size, e.g., to find registers at a
  5056. * fixed offset before the end of the BAR.
  5057. *
  5058. * 2) Retain the resource size, but use IORESOURCE_STARTALIGN and
  5059. * set r->start to the desired alignment. By itself this
  5060. * doesn't prevent other BARs being put inside the alignment
  5061. * region, but if we realign *every* resource of every device in
  5062. * the system, none of them will share an alignment region.
  5063. *
  5064. * When the user has requested alignment for only some devices via
  5065. * the "pci=resource_alignment" argument, "resize" is true and we
  5066. * use the first method. Otherwise we assume we're aligning all
  5067. * devices and we use the second.
  5068. */
  5069. pci_info(dev, "BAR%d %pR: requesting alignment to %#llx\n",
  5070. bar, r, (unsigned long long)align);
  5071. if (resize) {
  5072. r->start = 0;
  5073. r->end = align - 1;
  5074. } else {
  5075. r->flags &= ~IORESOURCE_SIZEALIGN;
  5076. r->flags |= IORESOURCE_STARTALIGN;
  5077. r->start = align;
  5078. r->end = r->start + size - 1;
  5079. }
  5080. r->flags |= IORESOURCE_UNSET;
  5081. }
  5082. /*
  5083. * This function disables memory decoding and releases memory resources
  5084. * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
  5085. * It also rounds up size to specified alignment.
  5086. * Later on, the kernel will assign page-aligned memory resource back
  5087. * to the device.
  5088. */
  5089. void pci_reassigndev_resource_alignment(struct pci_dev *dev)
  5090. {
  5091. int i;
  5092. struct resource *r;
  5093. resource_size_t align;
  5094. u16 command;
  5095. bool resize = false;
  5096. /*
  5097. * VF BARs are read-only zero according to SR-IOV spec r1.1, sec
  5098. * 3.4.1.11. Their resources are allocated from the space
  5099. * described by the VF BARx register in the PF's SR-IOV capability.
  5100. * We can't influence their alignment here.
  5101. */
  5102. if (dev->is_virtfn)
  5103. return;
  5104. /* check if specified PCI is target device to reassign */
  5105. align = pci_specified_resource_alignment(dev, &resize);
  5106. if (!align)
  5107. return;
  5108. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
  5109. (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
  5110. pci_warn(dev, "Can't reassign resources to host bridge\n");
  5111. return;
  5112. }
  5113. pci_read_config_word(dev, PCI_COMMAND, &command);
  5114. command &= ~PCI_COMMAND_MEMORY;
  5115. pci_write_config_word(dev, PCI_COMMAND, command);
  5116. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  5117. pci_request_resource_alignment(dev, i, align, resize);
  5118. /*
  5119. * Need to disable bridge's resource window,
  5120. * to enable the kernel to reassign new resource
  5121. * window later on.
  5122. */
  5123. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  5124. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  5125. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  5126. r = &dev->resource[i];
  5127. if (!(r->flags & IORESOURCE_MEM))
  5128. continue;
  5129. r->flags |= IORESOURCE_UNSET;
  5130. r->end = resource_size(r) - 1;
  5131. r->start = 0;
  5132. }
  5133. pci_disable_bridge_window(dev);
  5134. }
  5135. }
  5136. static ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
  5137. {
  5138. if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
  5139. count = RESOURCE_ALIGNMENT_PARAM_SIZE - 1;
  5140. spin_lock(&resource_alignment_lock);
  5141. strncpy(resource_alignment_param, buf, count);
  5142. resource_alignment_param[count] = '\0';
  5143. spin_unlock(&resource_alignment_lock);
  5144. return count;
  5145. }
  5146. static ssize_t pci_get_resource_alignment_param(char *buf, size_t size)
  5147. {
  5148. size_t count;
  5149. spin_lock(&resource_alignment_lock);
  5150. count = snprintf(buf, size, "%s", resource_alignment_param);
  5151. spin_unlock(&resource_alignment_lock);
  5152. return count;
  5153. }
  5154. static ssize_t pci_resource_alignment_show(struct bus_type *bus, char *buf)
  5155. {
  5156. return pci_get_resource_alignment_param(buf, PAGE_SIZE);
  5157. }
  5158. static ssize_t pci_resource_alignment_store(struct bus_type *bus,
  5159. const char *buf, size_t count)
  5160. {
  5161. return pci_set_resource_alignment_param(buf, count);
  5162. }
  5163. static BUS_ATTR(resource_alignment, 0644, pci_resource_alignment_show,
  5164. pci_resource_alignment_store);
  5165. static int __init pci_resource_alignment_sysfs_init(void)
  5166. {
  5167. return bus_create_file(&pci_bus_type,
  5168. &bus_attr_resource_alignment);
  5169. }
  5170. late_initcall(pci_resource_alignment_sysfs_init);
  5171. static void pci_no_domains(void)
  5172. {
  5173. #ifdef CONFIG_PCI_DOMAINS
  5174. pci_domains_supported = 0;
  5175. #endif
  5176. }
  5177. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  5178. static atomic_t __domain_nr = ATOMIC_INIT(-1);
  5179. static int pci_get_new_domain_nr(void)
  5180. {
  5181. return atomic_inc_return(&__domain_nr);
  5182. }
  5183. static int of_pci_bus_find_domain_nr(struct device *parent)
  5184. {
  5185. static int use_dt_domains = -1;
  5186. int domain = -1;
  5187. if (parent)
  5188. domain = of_get_pci_domain_nr(parent->of_node);
  5189. /*
  5190. * Check DT domain and use_dt_domains values.
  5191. *
  5192. * If DT domain property is valid (domain >= 0) and
  5193. * use_dt_domains != 0, the DT assignment is valid since this means
  5194. * we have not previously allocated a domain number by using
  5195. * pci_get_new_domain_nr(); we should also update use_dt_domains to
  5196. * 1, to indicate that we have just assigned a domain number from
  5197. * DT.
  5198. *
  5199. * If DT domain property value is not valid (ie domain < 0), and we
  5200. * have not previously assigned a domain number from DT
  5201. * (use_dt_domains != 1) we should assign a domain number by
  5202. * using the:
  5203. *
  5204. * pci_get_new_domain_nr()
  5205. *
  5206. * API and update the use_dt_domains value to keep track of method we
  5207. * are using to assign domain numbers (use_dt_domains = 0).
  5208. *
  5209. * All other combinations imply we have a platform that is trying
  5210. * to mix domain numbers obtained from DT and pci_get_new_domain_nr(),
  5211. * which is a recipe for domain mishandling and it is prevented by
  5212. * invalidating the domain value (domain = -1) and printing a
  5213. * corresponding error.
  5214. */
  5215. if (domain >= 0 && use_dt_domains) {
  5216. use_dt_domains = 1;
  5217. } else if (domain < 0 && use_dt_domains != 1) {
  5218. use_dt_domains = 0;
  5219. domain = pci_get_new_domain_nr();
  5220. } else {
  5221. if (parent)
  5222. pr_err("Node %pOF has ", parent->of_node);
  5223. pr_err("Inconsistent \"linux,pci-domain\" property in DT\n");
  5224. domain = -1;
  5225. }
  5226. return domain;
  5227. }
  5228. int pci_bus_find_domain_nr(struct pci_bus *bus, struct device *parent)
  5229. {
  5230. return acpi_disabled ? of_pci_bus_find_domain_nr(parent) :
  5231. acpi_pci_bus_find_domain_nr(bus);
  5232. }
  5233. #endif
  5234. /**
  5235. * pci_ext_cfg_avail - can we access extended PCI config space?
  5236. *
  5237. * Returns 1 if we can access PCI extended config space (offsets
  5238. * greater than 0xff). This is the default implementation. Architecture
  5239. * implementations can override this.
  5240. */
  5241. int __weak pci_ext_cfg_avail(void)
  5242. {
  5243. return 1;
  5244. }
  5245. void __weak pci_fixup_cardbus(struct pci_bus *bus)
  5246. {
  5247. }
  5248. EXPORT_SYMBOL(pci_fixup_cardbus);
  5249. static int __init pci_setup(char *str)
  5250. {
  5251. while (str) {
  5252. char *k = strchr(str, ',');
  5253. if (k)
  5254. *k++ = 0;
  5255. if (*str && (str = pcibios_setup(str)) && *str) {
  5256. if (!strcmp(str, "nomsi")) {
  5257. pci_no_msi();
  5258. } else if (!strncmp(str, "noats", 5)) {
  5259. pr_info("PCIe: ATS is disabled\n");
  5260. pcie_ats_disabled = true;
  5261. } else if (!strcmp(str, "noaer")) {
  5262. pci_no_aer();
  5263. } else if (!strcmp(str, "earlydump")) {
  5264. pci_early_dump = true;
  5265. } else if (!strncmp(str, "realloc=", 8)) {
  5266. pci_realloc_get_opt(str + 8);
  5267. } else if (!strncmp(str, "realloc", 7)) {
  5268. pci_realloc_get_opt("on");
  5269. } else if (!strcmp(str, "nodomains")) {
  5270. pci_no_domains();
  5271. } else if (!strncmp(str, "noari", 5)) {
  5272. pcie_ari_disabled = true;
  5273. } else if (!strncmp(str, "cbiosize=", 9)) {
  5274. pci_cardbus_io_size = memparse(str + 9, &str);
  5275. } else if (!strncmp(str, "cbmemsize=", 10)) {
  5276. pci_cardbus_mem_size = memparse(str + 10, &str);
  5277. } else if (!strncmp(str, "resource_alignment=", 19)) {
  5278. pci_set_resource_alignment_param(str + 19,
  5279. strlen(str + 19));
  5280. } else if (!strncmp(str, "ecrc=", 5)) {
  5281. pcie_ecrc_get_policy(str + 5);
  5282. } else if (!strncmp(str, "hpiosize=", 9)) {
  5283. pci_hotplug_io_size = memparse(str + 9, &str);
  5284. } else if (!strncmp(str, "hpmemsize=", 10)) {
  5285. pci_hotplug_mem_size = memparse(str + 10, &str);
  5286. } else if (!strncmp(str, "hpbussize=", 10)) {
  5287. pci_hotplug_bus_size =
  5288. simple_strtoul(str + 10, &str, 0);
  5289. if (pci_hotplug_bus_size > 0xff)
  5290. pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
  5291. } else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
  5292. pcie_bus_config = PCIE_BUS_TUNE_OFF;
  5293. } else if (!strncmp(str, "pcie_bus_safe", 13)) {
  5294. pcie_bus_config = PCIE_BUS_SAFE;
  5295. } else if (!strncmp(str, "pcie_bus_perf", 13)) {
  5296. pcie_bus_config = PCIE_BUS_PERFORMANCE;
  5297. } else if (!strncmp(str, "pcie_bus_peer2peer", 18)) {
  5298. pcie_bus_config = PCIE_BUS_PEER2PEER;
  5299. } else if (!strncmp(str, "pcie_scan_all", 13)) {
  5300. pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
  5301. } else if (!strncmp(str, "disable_acs_redir=", 18)) {
  5302. disable_acs_redir_param = str + 18;
  5303. } else {
  5304. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  5305. str);
  5306. }
  5307. }
  5308. str = k;
  5309. }
  5310. return 0;
  5311. }
  5312. early_param("pci", pci_setup);