pci.c 146 KB

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