pci.c 181 KB

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