pci.c 138 KB

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