pci.c 179 KB

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