pci.c 154 KB

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