pci.c 73 KB

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