pci.c 91 KB

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