pci.c 103 KB

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