pci.c 74 KB

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