pci.c 153 KB

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