pci.c 62 KB

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