pci.c 115 KB

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