pci.c 184 KB

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