pci.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  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. static 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. /**
  840. * pci_disable_device - Disable PCI device after use
  841. * @dev: PCI device to be disabled
  842. *
  843. * Signal to the system that the PCI device is not in use by the system
  844. * anymore. This only involves disabling PCI bus-mastering, if active.
  845. *
  846. * Note we don't actually disable the device until all callers of
  847. * pci_device_enable() have called pci_device_disable().
  848. */
  849. void
  850. pci_disable_device(struct pci_dev *dev)
  851. {
  852. struct pci_devres *dr;
  853. u16 pci_command;
  854. dr = find_pci_dr(dev);
  855. if (dr)
  856. dr->enabled = 0;
  857. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  858. return;
  859. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  860. if (pci_command & PCI_COMMAND_MASTER) {
  861. pci_command &= ~PCI_COMMAND_MASTER;
  862. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  863. }
  864. dev->is_busmaster = 0;
  865. pcibios_disable_device(dev);
  866. }
  867. /**
  868. * pcibios_set_pcie_reset_state - set reset state for device dev
  869. * @dev: the PCI-E device reset
  870. * @state: Reset state to enter into
  871. *
  872. *
  873. * Sets the PCI-E reset state for the device. This is the default
  874. * implementation. Architecture implementations can override this.
  875. */
  876. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  877. enum pcie_reset_state state)
  878. {
  879. return -EINVAL;
  880. }
  881. /**
  882. * pci_set_pcie_reset_state - set reset state for device dev
  883. * @dev: the PCI-E device reset
  884. * @state: Reset state to enter into
  885. *
  886. *
  887. * Sets the PCI reset state for the device.
  888. */
  889. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  890. {
  891. return pcibios_set_pcie_reset_state(dev, state);
  892. }
  893. /**
  894. * pci_pme_capable - check the capability of PCI device to generate PME#
  895. * @dev: PCI device to handle.
  896. * @state: PCI state from which device will issue PME#.
  897. */
  898. bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
  899. {
  900. if (!dev->pm_cap)
  901. return false;
  902. return !!(dev->pme_support & (1 << state));
  903. }
  904. /**
  905. * pci_pme_active - enable or disable PCI device's PME# function
  906. * @dev: PCI device to handle.
  907. * @enable: 'true' to enable PME# generation; 'false' to disable it.
  908. *
  909. * The caller must verify that the device is capable of generating PME# before
  910. * calling this function with @enable equal to 'true'.
  911. */
  912. void pci_pme_active(struct pci_dev *dev, bool enable)
  913. {
  914. u16 pmcsr;
  915. if (!dev->pm_cap)
  916. return;
  917. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
  918. /* Clear PME_Status by writing 1 to it and enable PME# */
  919. pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  920. if (!enable)
  921. pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
  922. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
  923. dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
  924. enable ? "enabled" : "disabled");
  925. }
  926. /**
  927. * pci_enable_wake - enable PCI device as wakeup event source
  928. * @dev: PCI device affected
  929. * @state: PCI state from which device will issue wakeup events
  930. * @enable: True to enable event generation; false to disable
  931. *
  932. * This enables the device as a wakeup event source, or disables it.
  933. * When such events involves platform-specific hooks, those hooks are
  934. * called automatically by this routine.
  935. *
  936. * Devices with legacy power management (no standard PCI PM capabilities)
  937. * always require such platform hooks.
  938. *
  939. * RETURN VALUE:
  940. * 0 is returned on success
  941. * -EINVAL is returned if device is not supposed to wake up the system
  942. * Error code depending on the platform is returned if both the platform and
  943. * the native mechanism fail to enable the generation of wake-up events
  944. */
  945. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  946. {
  947. int error = 0;
  948. bool pme_done = false;
  949. if (enable && !device_may_wakeup(&dev->dev))
  950. return -EINVAL;
  951. /*
  952. * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
  953. * Anderson we should be doing PME# wake enable followed by ACPI wake
  954. * enable. To disable wake-up we call the platform first, for symmetry.
  955. */
  956. if (!enable && platform_pci_can_wakeup(dev))
  957. error = platform_pci_sleep_wake(dev, false);
  958. if (!enable || pci_pme_capable(dev, state)) {
  959. pci_pme_active(dev, enable);
  960. pme_done = true;
  961. }
  962. if (enable && platform_pci_can_wakeup(dev))
  963. error = platform_pci_sleep_wake(dev, true);
  964. return pme_done ? 0 : error;
  965. }
  966. /**
  967. * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
  968. * @dev: PCI device to prepare
  969. * @enable: True to enable wake-up event generation; false to disable
  970. *
  971. * Many drivers want the device to wake up the system from D3_hot or D3_cold
  972. * and this function allows them to set that up cleanly - pci_enable_wake()
  973. * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
  974. * ordering constraints.
  975. *
  976. * This function only returns error code if the device is not capable of
  977. * generating PME# from both D3_hot and D3_cold, and the platform is unable to
  978. * enable wake-up power for it.
  979. */
  980. int pci_wake_from_d3(struct pci_dev *dev, bool enable)
  981. {
  982. return pci_pme_capable(dev, PCI_D3cold) ?
  983. pci_enable_wake(dev, PCI_D3cold, enable) :
  984. pci_enable_wake(dev, PCI_D3hot, enable);
  985. }
  986. /**
  987. * pci_target_state - find an appropriate low power state for a given PCI dev
  988. * @dev: PCI device
  989. *
  990. * Use underlying platform code to find a supported low power state for @dev.
  991. * If the platform can't manage @dev, return the deepest state from which it
  992. * can generate wake events, based on any available PME info.
  993. */
  994. pci_power_t pci_target_state(struct pci_dev *dev)
  995. {
  996. pci_power_t target_state = PCI_D3hot;
  997. if (platform_pci_power_manageable(dev)) {
  998. /*
  999. * Call the platform to choose the target state of the device
  1000. * and enable wake-up from this state if supported.
  1001. */
  1002. pci_power_t state = platform_pci_choose_state(dev);
  1003. switch (state) {
  1004. case PCI_POWER_ERROR:
  1005. case PCI_UNKNOWN:
  1006. break;
  1007. case PCI_D1:
  1008. case PCI_D2:
  1009. if (pci_no_d1d2(dev))
  1010. break;
  1011. default:
  1012. target_state = state;
  1013. }
  1014. } else if (device_may_wakeup(&dev->dev)) {
  1015. /*
  1016. * Find the deepest state from which the device can generate
  1017. * wake-up events, make it the target state and enable device
  1018. * to generate PME#.
  1019. */
  1020. if (!dev->pm_cap)
  1021. return PCI_POWER_ERROR;
  1022. if (dev->pme_support) {
  1023. while (target_state
  1024. && !(dev->pme_support & (1 << target_state)))
  1025. target_state--;
  1026. }
  1027. }
  1028. return target_state;
  1029. }
  1030. /**
  1031. * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
  1032. * @dev: Device to handle.
  1033. *
  1034. * Choose the power state appropriate for the device depending on whether
  1035. * it can wake up the system and/or is power manageable by the platform
  1036. * (PCI_D3hot is the default) and put the device into that state.
  1037. */
  1038. int pci_prepare_to_sleep(struct pci_dev *dev)
  1039. {
  1040. pci_power_t target_state = pci_target_state(dev);
  1041. int error;
  1042. if (target_state == PCI_POWER_ERROR)
  1043. return -EIO;
  1044. pci_enable_wake(dev, target_state, true);
  1045. error = pci_set_power_state(dev, target_state);
  1046. if (error)
  1047. pci_enable_wake(dev, target_state, false);
  1048. return error;
  1049. }
  1050. /**
  1051. * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
  1052. * @dev: Device to handle.
  1053. *
  1054. * Disable device's sytem wake-up capability and put it into D0.
  1055. */
  1056. int pci_back_from_sleep(struct pci_dev *dev)
  1057. {
  1058. pci_enable_wake(dev, PCI_D0, false);
  1059. return pci_set_power_state(dev, PCI_D0);
  1060. }
  1061. /**
  1062. * pci_pm_init - Initialize PM functions of given PCI device
  1063. * @dev: PCI device to handle.
  1064. */
  1065. void pci_pm_init(struct pci_dev *dev)
  1066. {
  1067. int pm;
  1068. u16 pmc;
  1069. dev->pm_cap = 0;
  1070. /* find PCI PM capability in list */
  1071. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1072. if (!pm)
  1073. return;
  1074. /* Check device's ability to generate PME# */
  1075. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1076. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  1077. dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
  1078. pmc & PCI_PM_CAP_VER_MASK);
  1079. return;
  1080. }
  1081. dev->pm_cap = pm;
  1082. dev->d1_support = false;
  1083. dev->d2_support = false;
  1084. if (!pci_no_d1d2(dev)) {
  1085. if (pmc & PCI_PM_CAP_D1)
  1086. dev->d1_support = true;
  1087. if (pmc & PCI_PM_CAP_D2)
  1088. dev->d2_support = true;
  1089. if (dev->d1_support || dev->d2_support)
  1090. dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
  1091. dev->d1_support ? " D1" : "",
  1092. dev->d2_support ? " D2" : "");
  1093. }
  1094. pmc &= PCI_PM_CAP_PME_MASK;
  1095. if (pmc) {
  1096. dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n",
  1097. (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
  1098. (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
  1099. (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
  1100. (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
  1101. (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
  1102. dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
  1103. /*
  1104. * Make device's PM flags reflect the wake-up capability, but
  1105. * let the user space enable it to wake up the system as needed.
  1106. */
  1107. device_set_wakeup_capable(&dev->dev, true);
  1108. device_set_wakeup_enable(&dev->dev, false);
  1109. /* Disable the PME# generation functionality */
  1110. pci_pme_active(dev, false);
  1111. } else {
  1112. dev->pme_support = 0;
  1113. }
  1114. }
  1115. /**
  1116. * platform_pci_wakeup_init - init platform wakeup if present
  1117. * @dev: PCI device
  1118. *
  1119. * Some devices don't have PCI PM caps but can still generate wakeup
  1120. * events through platform methods (like ACPI events). If @dev supports
  1121. * platform wakeup events, set the device flag to indicate as much. This
  1122. * may be redundant if the device also supports PCI PM caps, but double
  1123. * initialization should be safe in that case.
  1124. */
  1125. void platform_pci_wakeup_init(struct pci_dev *dev)
  1126. {
  1127. if (!platform_pci_can_wakeup(dev))
  1128. return;
  1129. device_set_wakeup_capable(&dev->dev, true);
  1130. device_set_wakeup_enable(&dev->dev, false);
  1131. platform_pci_sleep_wake(dev, false);
  1132. }
  1133. /**
  1134. * pci_add_save_buffer - allocate buffer for saving given capability registers
  1135. * @dev: the PCI device
  1136. * @cap: the capability to allocate the buffer for
  1137. * @size: requested size of the buffer
  1138. */
  1139. static int pci_add_cap_save_buffer(
  1140. struct pci_dev *dev, char cap, unsigned int size)
  1141. {
  1142. int pos;
  1143. struct pci_cap_saved_state *save_state;
  1144. pos = pci_find_capability(dev, cap);
  1145. if (pos <= 0)
  1146. return 0;
  1147. save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
  1148. if (!save_state)
  1149. return -ENOMEM;
  1150. save_state->cap_nr = cap;
  1151. pci_add_saved_cap(dev, save_state);
  1152. return 0;
  1153. }
  1154. /**
  1155. * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
  1156. * @dev: the PCI device
  1157. */
  1158. void pci_allocate_cap_save_buffers(struct pci_dev *dev)
  1159. {
  1160. int error;
  1161. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 4 * sizeof(u16));
  1162. if (error)
  1163. dev_err(&dev->dev,
  1164. "unable to preallocate PCI Express save buffer\n");
  1165. error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
  1166. if (error)
  1167. dev_err(&dev->dev,
  1168. "unable to preallocate PCI-X save buffer\n");
  1169. }
  1170. /**
  1171. * pci_enable_ari - enable ARI forwarding if hardware support it
  1172. * @dev: the PCI device
  1173. */
  1174. void pci_enable_ari(struct pci_dev *dev)
  1175. {
  1176. int pos;
  1177. u32 cap;
  1178. u16 ctrl;
  1179. struct pci_dev *bridge;
  1180. if (!dev->is_pcie || dev->devfn)
  1181. return;
  1182. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1183. if (!pos)
  1184. return;
  1185. bridge = dev->bus->self;
  1186. if (!bridge || !bridge->is_pcie)
  1187. return;
  1188. pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  1189. if (!pos)
  1190. return;
  1191. pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
  1192. if (!(cap & PCI_EXP_DEVCAP2_ARI))
  1193. return;
  1194. pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
  1195. ctrl |= PCI_EXP_DEVCTL2_ARI;
  1196. pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
  1197. bridge->ari_enabled = 1;
  1198. }
  1199. /**
  1200. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  1201. * @dev: the PCI device
  1202. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  1203. *
  1204. * Perform INTx swizzling for a device behind one level of bridge. This is
  1205. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  1206. * behind bridges on add-in cards.
  1207. */
  1208. u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
  1209. {
  1210. return (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
  1211. }
  1212. int
  1213. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  1214. {
  1215. u8 pin;
  1216. pin = dev->pin;
  1217. if (!pin)
  1218. return -1;
  1219. while (dev->bus->self) {
  1220. pin = pci_swizzle_interrupt_pin(dev, pin);
  1221. dev = dev->bus->self;
  1222. }
  1223. *bridge = dev;
  1224. return pin;
  1225. }
  1226. /**
  1227. * pci_release_region - Release a PCI bar
  1228. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  1229. * @bar: BAR to release
  1230. *
  1231. * Releases the PCI I/O and memory resources previously reserved by a
  1232. * successful call to pci_request_region. Call this function only
  1233. * after all use of the PCI regions has ceased.
  1234. */
  1235. void pci_release_region(struct pci_dev *pdev, int bar)
  1236. {
  1237. struct pci_devres *dr;
  1238. if (pci_resource_len(pdev, bar) == 0)
  1239. return;
  1240. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  1241. release_region(pci_resource_start(pdev, bar),
  1242. pci_resource_len(pdev, bar));
  1243. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  1244. release_mem_region(pci_resource_start(pdev, bar),
  1245. pci_resource_len(pdev, bar));
  1246. dr = find_pci_dr(pdev);
  1247. if (dr)
  1248. dr->region_mask &= ~(1 << bar);
  1249. }
  1250. /**
  1251. * pci_request_region - Reserved PCI I/O and memory resource
  1252. * @pdev: PCI device whose resources are to be reserved
  1253. * @bar: BAR to be reserved
  1254. * @res_name: Name to be associated with resource.
  1255. *
  1256. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1257. * being reserved by owner @res_name. Do not access any
  1258. * address inside the PCI regions unless this call returns
  1259. * successfully.
  1260. *
  1261. * Returns 0 on success, or %EBUSY on error. A warning
  1262. * message is also printed on failure.
  1263. */
  1264. static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
  1265. int exclusive)
  1266. {
  1267. struct pci_devres *dr;
  1268. if (pci_resource_len(pdev, bar) == 0)
  1269. return 0;
  1270. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  1271. if (!request_region(pci_resource_start(pdev, bar),
  1272. pci_resource_len(pdev, bar), res_name))
  1273. goto err_out;
  1274. }
  1275. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  1276. if (!__request_mem_region(pci_resource_start(pdev, bar),
  1277. pci_resource_len(pdev, bar), res_name,
  1278. exclusive))
  1279. goto err_out;
  1280. }
  1281. dr = find_pci_dr(pdev);
  1282. if (dr)
  1283. dr->region_mask |= 1 << bar;
  1284. return 0;
  1285. err_out:
  1286. dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
  1287. bar,
  1288. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1289. &pdev->resource[bar]);
  1290. return -EBUSY;
  1291. }
  1292. /**
  1293. * pci_request_region - Reserved PCI I/O and memory resource
  1294. * @pdev: PCI device whose resources are to be reserved
  1295. * @bar: BAR to be reserved
  1296. * @res_name: Name to be associated with resource.
  1297. *
  1298. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1299. * being reserved by owner @res_name. Do not access any
  1300. * address inside the PCI regions unless this call returns
  1301. * successfully.
  1302. *
  1303. * Returns 0 on success, or %EBUSY on error. A warning
  1304. * message is also printed on failure.
  1305. */
  1306. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  1307. {
  1308. return __pci_request_region(pdev, bar, res_name, 0);
  1309. }
  1310. /**
  1311. * pci_request_region_exclusive - Reserved PCI I/O and memory resource
  1312. * @pdev: PCI device whose resources are to be reserved
  1313. * @bar: BAR to be reserved
  1314. * @res_name: Name to be associated with resource.
  1315. *
  1316. * Mark the PCI region associated with PCI device @pdev BR @bar as
  1317. * being reserved by owner @res_name. Do not access any
  1318. * address inside the PCI regions unless this call returns
  1319. * successfully.
  1320. *
  1321. * Returns 0 on success, or %EBUSY on error. A warning
  1322. * message is also printed on failure.
  1323. *
  1324. * The key difference that _exclusive makes it that userspace is
  1325. * explicitly not allowed to map the resource via /dev/mem or
  1326. * sysfs.
  1327. */
  1328. int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
  1329. {
  1330. return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
  1331. }
  1332. /**
  1333. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1334. * @pdev: PCI device whose resources were previously reserved
  1335. * @bars: Bitmask of BARs to be released
  1336. *
  1337. * Release selected PCI I/O and memory resources previously reserved.
  1338. * Call this function only after all use of the PCI regions has ceased.
  1339. */
  1340. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1341. {
  1342. int i;
  1343. for (i = 0; i < 6; i++)
  1344. if (bars & (1 << i))
  1345. pci_release_region(pdev, i);
  1346. }
  1347. int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1348. const char *res_name, int excl)
  1349. {
  1350. int i;
  1351. for (i = 0; i < 6; i++)
  1352. if (bars & (1 << i))
  1353. if (__pci_request_region(pdev, i, res_name, excl))
  1354. goto err_out;
  1355. return 0;
  1356. err_out:
  1357. while(--i >= 0)
  1358. if (bars & (1 << i))
  1359. pci_release_region(pdev, i);
  1360. return -EBUSY;
  1361. }
  1362. /**
  1363. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1364. * @pdev: PCI device whose resources are to be reserved
  1365. * @bars: Bitmask of BARs to be requested
  1366. * @res_name: Name to be associated with resource
  1367. */
  1368. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1369. const char *res_name)
  1370. {
  1371. return __pci_request_selected_regions(pdev, bars, res_name, 0);
  1372. }
  1373. int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
  1374. int bars, const char *res_name)
  1375. {
  1376. return __pci_request_selected_regions(pdev, bars, res_name,
  1377. IORESOURCE_EXCLUSIVE);
  1378. }
  1379. /**
  1380. * pci_release_regions - Release reserved PCI I/O and memory resources
  1381. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1382. *
  1383. * Releases all PCI I/O and memory resources previously reserved by a
  1384. * successful call to pci_request_regions. Call this function only
  1385. * after all use of the PCI regions has ceased.
  1386. */
  1387. void pci_release_regions(struct pci_dev *pdev)
  1388. {
  1389. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1390. }
  1391. /**
  1392. * pci_request_regions - Reserved PCI I/O and memory resources
  1393. * @pdev: PCI device whose resources are to be reserved
  1394. * @res_name: Name to be associated with resource.
  1395. *
  1396. * Mark all PCI regions associated with PCI device @pdev as
  1397. * being reserved by owner @res_name. Do not access any
  1398. * address inside the PCI regions unless this call returns
  1399. * successfully.
  1400. *
  1401. * Returns 0 on success, or %EBUSY on error. A warning
  1402. * message is also printed on failure.
  1403. */
  1404. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1405. {
  1406. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1407. }
  1408. /**
  1409. * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
  1410. * @pdev: PCI device whose resources are to be reserved
  1411. * @res_name: Name to be associated with resource.
  1412. *
  1413. * Mark all PCI regions associated with PCI device @pdev as
  1414. * being reserved by owner @res_name. Do not access any
  1415. * address inside the PCI regions unless this call returns
  1416. * successfully.
  1417. *
  1418. * pci_request_regions_exclusive() will mark the region so that
  1419. * /dev/mem and the sysfs MMIO access will not be allowed.
  1420. *
  1421. * Returns 0 on success, or %EBUSY on error. A warning
  1422. * message is also printed on failure.
  1423. */
  1424. int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
  1425. {
  1426. return pci_request_selected_regions_exclusive(pdev,
  1427. ((1 << 6) - 1), res_name);
  1428. }
  1429. /**
  1430. * pci_set_master - enables bus-mastering for device dev
  1431. * @dev: the PCI device to enable
  1432. *
  1433. * Enables bus-mastering on the device and calls pcibios_set_master()
  1434. * to do the needed arch specific settings.
  1435. */
  1436. void
  1437. pci_set_master(struct pci_dev *dev)
  1438. {
  1439. u16 cmd;
  1440. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1441. if (! (cmd & PCI_COMMAND_MASTER)) {
  1442. dev_dbg(&dev->dev, "enabling bus mastering\n");
  1443. cmd |= PCI_COMMAND_MASTER;
  1444. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1445. }
  1446. dev->is_busmaster = 1;
  1447. pcibios_set_master(dev);
  1448. }
  1449. #ifdef PCI_DISABLE_MWI
  1450. int pci_set_mwi(struct pci_dev *dev)
  1451. {
  1452. return 0;
  1453. }
  1454. int pci_try_set_mwi(struct pci_dev *dev)
  1455. {
  1456. return 0;
  1457. }
  1458. void pci_clear_mwi(struct pci_dev *dev)
  1459. {
  1460. }
  1461. #else
  1462. #ifndef PCI_CACHE_LINE_BYTES
  1463. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1464. #endif
  1465. /* This can be overridden by arch code. */
  1466. /* Don't forget this is measured in 32-bit words, not bytes */
  1467. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1468. /**
  1469. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1470. * @dev: the PCI device for which MWI is to be enabled
  1471. *
  1472. * Helper function for pci_set_mwi.
  1473. * Originally copied from drivers/net/acenic.c.
  1474. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1475. *
  1476. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1477. */
  1478. static int
  1479. pci_set_cacheline_size(struct pci_dev *dev)
  1480. {
  1481. u8 cacheline_size;
  1482. if (!pci_cache_line_size)
  1483. return -EINVAL; /* The system doesn't support MWI. */
  1484. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1485. equal to or multiple of the right value. */
  1486. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1487. if (cacheline_size >= pci_cache_line_size &&
  1488. (cacheline_size % pci_cache_line_size) == 0)
  1489. return 0;
  1490. /* Write the correct value. */
  1491. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1492. /* Read it back. */
  1493. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1494. if (cacheline_size == pci_cache_line_size)
  1495. return 0;
  1496. dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
  1497. "supported\n", pci_cache_line_size << 2);
  1498. return -EINVAL;
  1499. }
  1500. /**
  1501. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1502. * @dev: the PCI device for which MWI is enabled
  1503. *
  1504. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1505. *
  1506. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1507. */
  1508. int
  1509. pci_set_mwi(struct pci_dev *dev)
  1510. {
  1511. int rc;
  1512. u16 cmd;
  1513. rc = pci_set_cacheline_size(dev);
  1514. if (rc)
  1515. return rc;
  1516. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1517. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1518. dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
  1519. cmd |= PCI_COMMAND_INVALIDATE;
  1520. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1521. }
  1522. return 0;
  1523. }
  1524. /**
  1525. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1526. * @dev: the PCI device for which MWI is enabled
  1527. *
  1528. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1529. * Callers are not required to check the return value.
  1530. *
  1531. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1532. */
  1533. int pci_try_set_mwi(struct pci_dev *dev)
  1534. {
  1535. int rc = pci_set_mwi(dev);
  1536. return rc;
  1537. }
  1538. /**
  1539. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1540. * @dev: the PCI device to disable
  1541. *
  1542. * Disables PCI Memory-Write-Invalidate transaction on the device
  1543. */
  1544. void
  1545. pci_clear_mwi(struct pci_dev *dev)
  1546. {
  1547. u16 cmd;
  1548. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1549. if (cmd & PCI_COMMAND_INVALIDATE) {
  1550. cmd &= ~PCI_COMMAND_INVALIDATE;
  1551. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1552. }
  1553. }
  1554. #endif /* ! PCI_DISABLE_MWI */
  1555. /**
  1556. * pci_intx - enables/disables PCI INTx for device dev
  1557. * @pdev: the PCI device to operate on
  1558. * @enable: boolean: whether to enable or disable PCI INTx
  1559. *
  1560. * Enables/disables PCI INTx for device dev
  1561. */
  1562. void
  1563. pci_intx(struct pci_dev *pdev, int enable)
  1564. {
  1565. u16 pci_command, new;
  1566. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1567. if (enable) {
  1568. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1569. } else {
  1570. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1571. }
  1572. if (new != pci_command) {
  1573. struct pci_devres *dr;
  1574. pci_write_config_word(pdev, PCI_COMMAND, new);
  1575. dr = find_pci_dr(pdev);
  1576. if (dr && !dr->restore_intx) {
  1577. dr->restore_intx = 1;
  1578. dr->orig_intx = !enable;
  1579. }
  1580. }
  1581. }
  1582. /**
  1583. * pci_msi_off - disables any msi or msix capabilities
  1584. * @dev: the PCI device to operate on
  1585. *
  1586. * If you want to use msi see pci_enable_msi and friends.
  1587. * This is a lower level primitive that allows us to disable
  1588. * msi operation at the device level.
  1589. */
  1590. void pci_msi_off(struct pci_dev *dev)
  1591. {
  1592. int pos;
  1593. u16 control;
  1594. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1595. if (pos) {
  1596. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1597. control &= ~PCI_MSI_FLAGS_ENABLE;
  1598. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1599. }
  1600. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1601. if (pos) {
  1602. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1603. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1604. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1605. }
  1606. }
  1607. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1608. /*
  1609. * These can be overridden by arch-specific implementations
  1610. */
  1611. int
  1612. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1613. {
  1614. if (!pci_dma_supported(dev, mask))
  1615. return -EIO;
  1616. dev->dma_mask = mask;
  1617. return 0;
  1618. }
  1619. int
  1620. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1621. {
  1622. if (!pci_dma_supported(dev, mask))
  1623. return -EIO;
  1624. dev->dev.coherent_dma_mask = mask;
  1625. return 0;
  1626. }
  1627. #endif
  1628. #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
  1629. int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
  1630. {
  1631. return dma_set_max_seg_size(&dev->dev, size);
  1632. }
  1633. EXPORT_SYMBOL(pci_set_dma_max_seg_size);
  1634. #endif
  1635. #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
  1636. int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
  1637. {
  1638. return dma_set_seg_boundary(&dev->dev, mask);
  1639. }
  1640. EXPORT_SYMBOL(pci_set_dma_seg_boundary);
  1641. #endif
  1642. static int __pcie_flr(struct pci_dev *dev, int probe)
  1643. {
  1644. u16 status;
  1645. u32 cap;
  1646. int exppos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1647. if (!exppos)
  1648. return -ENOTTY;
  1649. pci_read_config_dword(dev, exppos + PCI_EXP_DEVCAP, &cap);
  1650. if (!(cap & PCI_EXP_DEVCAP_FLR))
  1651. return -ENOTTY;
  1652. if (probe)
  1653. return 0;
  1654. pci_block_user_cfg_access(dev);
  1655. /* Wait for Transaction Pending bit clean */
  1656. msleep(100);
  1657. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1658. if (status & PCI_EXP_DEVSTA_TRPND) {
  1659. dev_info(&dev->dev, "Busy after 100ms while trying to reset; "
  1660. "sleeping for 1 second\n");
  1661. ssleep(1);
  1662. pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
  1663. if (status & PCI_EXP_DEVSTA_TRPND)
  1664. dev_info(&dev->dev, "Still busy after 1s; "
  1665. "proceeding with reset anyway\n");
  1666. }
  1667. pci_write_config_word(dev, exppos + PCI_EXP_DEVCTL,
  1668. PCI_EXP_DEVCTL_BCR_FLR);
  1669. mdelay(100);
  1670. pci_unblock_user_cfg_access(dev);
  1671. return 0;
  1672. }
  1673. static int __pci_af_flr(struct pci_dev *dev, int probe)
  1674. {
  1675. int cappos = pci_find_capability(dev, PCI_CAP_ID_AF);
  1676. u8 status;
  1677. u8 cap;
  1678. if (!cappos)
  1679. return -ENOTTY;
  1680. pci_read_config_byte(dev, cappos + PCI_AF_CAP, &cap);
  1681. if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
  1682. return -ENOTTY;
  1683. if (probe)
  1684. return 0;
  1685. pci_block_user_cfg_access(dev);
  1686. /* Wait for Transaction Pending bit clean */
  1687. msleep(100);
  1688. pci_read_config_byte(dev, cappos + PCI_AF_STATUS, &status);
  1689. if (status & PCI_AF_STATUS_TP) {
  1690. dev_info(&dev->dev, "Busy after 100ms while trying to"
  1691. " reset; sleeping for 1 second\n");
  1692. ssleep(1);
  1693. pci_read_config_byte(dev,
  1694. cappos + PCI_AF_STATUS, &status);
  1695. if (status & PCI_AF_STATUS_TP)
  1696. dev_info(&dev->dev, "Still busy after 1s; "
  1697. "proceeding with reset anyway\n");
  1698. }
  1699. pci_write_config_byte(dev, cappos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
  1700. mdelay(100);
  1701. pci_unblock_user_cfg_access(dev);
  1702. return 0;
  1703. }
  1704. static int __pci_reset_function(struct pci_dev *pdev, int probe)
  1705. {
  1706. int res;
  1707. res = __pcie_flr(pdev, probe);
  1708. if (res != -ENOTTY)
  1709. return res;
  1710. res = __pci_af_flr(pdev, probe);
  1711. if (res != -ENOTTY)
  1712. return res;
  1713. return res;
  1714. }
  1715. /**
  1716. * pci_execute_reset_function() - Reset a PCI device function
  1717. * @dev: Device function to reset
  1718. *
  1719. * Some devices allow an individual function to be reset without affecting
  1720. * other functions in the same device. The PCI device must be responsive
  1721. * to PCI config space in order to use this function.
  1722. *
  1723. * The device function is presumed to be unused when this function is called.
  1724. * Resetting the device will make the contents of PCI configuration space
  1725. * random, so any caller of this must be prepared to reinitialise the
  1726. * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
  1727. * etc.
  1728. *
  1729. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1730. * device doesn't support resetting a single function.
  1731. */
  1732. int pci_execute_reset_function(struct pci_dev *dev)
  1733. {
  1734. return __pci_reset_function(dev, 0);
  1735. }
  1736. EXPORT_SYMBOL_GPL(pci_execute_reset_function);
  1737. /**
  1738. * pci_reset_function() - quiesce and reset a PCI device function
  1739. * @dev: Device function to reset
  1740. *
  1741. * Some devices allow an individual function to be reset without affecting
  1742. * other functions in the same device. The PCI device must be responsive
  1743. * to PCI config space in order to use this function.
  1744. *
  1745. * This function does not just reset the PCI portion of a device, but
  1746. * clears all the state associated with the device. This function differs
  1747. * from pci_execute_reset_function in that it saves and restores device state
  1748. * over the reset.
  1749. *
  1750. * Returns 0 if the device function was successfully reset or -ENOTTY if the
  1751. * device doesn't support resetting a single function.
  1752. */
  1753. int pci_reset_function(struct pci_dev *dev)
  1754. {
  1755. int r = __pci_reset_function(dev, 1);
  1756. if (r < 0)
  1757. return r;
  1758. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1759. disable_irq(dev->irq);
  1760. pci_save_state(dev);
  1761. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
  1762. r = pci_execute_reset_function(dev);
  1763. pci_restore_state(dev);
  1764. if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
  1765. enable_irq(dev->irq);
  1766. return r;
  1767. }
  1768. EXPORT_SYMBOL_GPL(pci_reset_function);
  1769. /**
  1770. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1771. * @dev: PCI device to query
  1772. *
  1773. * Returns mmrbc: maximum designed memory read count in bytes
  1774. * or appropriate error value.
  1775. */
  1776. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1777. {
  1778. int err, cap;
  1779. u32 stat;
  1780. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1781. if (!cap)
  1782. return -EINVAL;
  1783. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1784. if (err)
  1785. return -EINVAL;
  1786. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1787. }
  1788. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1789. /**
  1790. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1791. * @dev: PCI device to query
  1792. *
  1793. * Returns mmrbc: maximum memory read count in bytes
  1794. * or appropriate error value.
  1795. */
  1796. int pcix_get_mmrbc(struct pci_dev *dev)
  1797. {
  1798. int ret, cap;
  1799. u32 cmd;
  1800. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1801. if (!cap)
  1802. return -EINVAL;
  1803. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1804. if (!ret)
  1805. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  1806. return ret;
  1807. }
  1808. EXPORT_SYMBOL(pcix_get_mmrbc);
  1809. /**
  1810. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  1811. * @dev: PCI device to query
  1812. * @mmrbc: maximum memory read count in bytes
  1813. * valid values are 512, 1024, 2048, 4096
  1814. *
  1815. * If possible sets maximum memory read byte count, some bridges have erratas
  1816. * that prevent this.
  1817. */
  1818. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  1819. {
  1820. int cap, err = -EINVAL;
  1821. u32 stat, cmd, v, o;
  1822. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  1823. goto out;
  1824. v = ffs(mmrbc) - 10;
  1825. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1826. if (!cap)
  1827. goto out;
  1828. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1829. if (err)
  1830. goto out;
  1831. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  1832. return -E2BIG;
  1833. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1834. if (err)
  1835. goto out;
  1836. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  1837. if (o != v) {
  1838. if (v > o && dev->bus &&
  1839. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  1840. return -EIO;
  1841. cmd &= ~PCI_X_CMD_MAX_READ;
  1842. cmd |= v << 2;
  1843. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  1844. }
  1845. out:
  1846. return err;
  1847. }
  1848. EXPORT_SYMBOL(pcix_set_mmrbc);
  1849. /**
  1850. * pcie_get_readrq - get PCI Express read request size
  1851. * @dev: PCI device to query
  1852. *
  1853. * Returns maximum memory read request in bytes
  1854. * or appropriate error value.
  1855. */
  1856. int pcie_get_readrq(struct pci_dev *dev)
  1857. {
  1858. int ret, cap;
  1859. u16 ctl;
  1860. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1861. if (!cap)
  1862. return -EINVAL;
  1863. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1864. if (!ret)
  1865. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1866. return ret;
  1867. }
  1868. EXPORT_SYMBOL(pcie_get_readrq);
  1869. /**
  1870. * pcie_set_readrq - set PCI Express maximum memory read request
  1871. * @dev: PCI device to query
  1872. * @rq: maximum memory read count in bytes
  1873. * valid values are 128, 256, 512, 1024, 2048, 4096
  1874. *
  1875. * If possible sets maximum read byte count
  1876. */
  1877. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1878. {
  1879. int cap, err = -EINVAL;
  1880. u16 ctl, v;
  1881. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1882. goto out;
  1883. v = (ffs(rq) - 8) << 12;
  1884. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1885. if (!cap)
  1886. goto out;
  1887. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1888. if (err)
  1889. goto out;
  1890. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  1891. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  1892. ctl |= v;
  1893. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  1894. }
  1895. out:
  1896. return err;
  1897. }
  1898. EXPORT_SYMBOL(pcie_set_readrq);
  1899. /**
  1900. * pci_select_bars - Make BAR mask from the type of resource
  1901. * @dev: the PCI device for which BAR mask is made
  1902. * @flags: resource type mask to be selected
  1903. *
  1904. * This helper routine makes bar mask from the type of resource.
  1905. */
  1906. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  1907. {
  1908. int i, bars = 0;
  1909. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1910. if (pci_resource_flags(dev, i) & flags)
  1911. bars |= (1 << i);
  1912. return bars;
  1913. }
  1914. /**
  1915. * pci_resource_bar - get position of the BAR associated with a resource
  1916. * @dev: the PCI device
  1917. * @resno: the resource number
  1918. * @type: the BAR type to be filled in
  1919. *
  1920. * Returns BAR position in config space, or 0 if the BAR is invalid.
  1921. */
  1922. int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
  1923. {
  1924. if (resno < PCI_ROM_RESOURCE) {
  1925. *type = pci_bar_unknown;
  1926. return PCI_BASE_ADDRESS_0 + 4 * resno;
  1927. } else if (resno == PCI_ROM_RESOURCE) {
  1928. *type = pci_bar_mem32;
  1929. return dev->rom_base_reg;
  1930. }
  1931. dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
  1932. return 0;
  1933. }
  1934. static void __devinit pci_no_domains(void)
  1935. {
  1936. #ifdef CONFIG_PCI_DOMAINS
  1937. pci_domains_supported = 0;
  1938. #endif
  1939. }
  1940. /**
  1941. * pci_ext_cfg_enabled - can we access extended PCI config space?
  1942. * @dev: The PCI device of the root bridge.
  1943. *
  1944. * Returns 1 if we can access PCI extended config space (offsets
  1945. * greater than 0xff). This is the default implementation. Architecture
  1946. * implementations can override this.
  1947. */
  1948. int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
  1949. {
  1950. return 1;
  1951. }
  1952. static int __devinit pci_init(void)
  1953. {
  1954. struct pci_dev *dev = NULL;
  1955. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1956. pci_fixup_device(pci_fixup_final, dev);
  1957. }
  1958. return 0;
  1959. }
  1960. static int __init pci_setup(char *str)
  1961. {
  1962. while (str) {
  1963. char *k = strchr(str, ',');
  1964. if (k)
  1965. *k++ = 0;
  1966. if (*str && (str = pcibios_setup(str)) && *str) {
  1967. if (!strcmp(str, "nomsi")) {
  1968. pci_no_msi();
  1969. } else if (!strcmp(str, "noaer")) {
  1970. pci_no_aer();
  1971. } else if (!strcmp(str, "nodomains")) {
  1972. pci_no_domains();
  1973. } else if (!strncmp(str, "cbiosize=", 9)) {
  1974. pci_cardbus_io_size = memparse(str + 9, &str);
  1975. } else if (!strncmp(str, "cbmemsize=", 10)) {
  1976. pci_cardbus_mem_size = memparse(str + 10, &str);
  1977. } else {
  1978. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  1979. str);
  1980. }
  1981. }
  1982. str = k;
  1983. }
  1984. return 0;
  1985. }
  1986. early_param("pci", pci_setup);
  1987. device_initcall(pci_init);
  1988. EXPORT_SYMBOL(pci_reenable_device);
  1989. EXPORT_SYMBOL(pci_enable_device_io);
  1990. EXPORT_SYMBOL(pci_enable_device_mem);
  1991. EXPORT_SYMBOL(pci_enable_device);
  1992. EXPORT_SYMBOL(pcim_enable_device);
  1993. EXPORT_SYMBOL(pcim_pin_device);
  1994. EXPORT_SYMBOL(pci_disable_device);
  1995. EXPORT_SYMBOL(pci_find_capability);
  1996. EXPORT_SYMBOL(pci_bus_find_capability);
  1997. EXPORT_SYMBOL(pci_release_regions);
  1998. EXPORT_SYMBOL(pci_request_regions);
  1999. EXPORT_SYMBOL(pci_request_regions_exclusive);
  2000. EXPORT_SYMBOL(pci_release_region);
  2001. EXPORT_SYMBOL(pci_request_region);
  2002. EXPORT_SYMBOL(pci_request_region_exclusive);
  2003. EXPORT_SYMBOL(pci_release_selected_regions);
  2004. EXPORT_SYMBOL(pci_request_selected_regions);
  2005. EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
  2006. EXPORT_SYMBOL(pci_set_master);
  2007. EXPORT_SYMBOL(pci_set_mwi);
  2008. EXPORT_SYMBOL(pci_try_set_mwi);
  2009. EXPORT_SYMBOL(pci_clear_mwi);
  2010. EXPORT_SYMBOL_GPL(pci_intx);
  2011. EXPORT_SYMBOL(pci_set_dma_mask);
  2012. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  2013. EXPORT_SYMBOL(pci_assign_resource);
  2014. EXPORT_SYMBOL(pci_find_parent_resource);
  2015. EXPORT_SYMBOL(pci_select_bars);
  2016. EXPORT_SYMBOL(pci_set_power_state);
  2017. EXPORT_SYMBOL(pci_save_state);
  2018. EXPORT_SYMBOL(pci_restore_state);
  2019. EXPORT_SYMBOL(pci_pme_capable);
  2020. EXPORT_SYMBOL(pci_pme_active);
  2021. EXPORT_SYMBOL(pci_enable_wake);
  2022. EXPORT_SYMBOL(pci_wake_from_d3);
  2023. EXPORT_SYMBOL(pci_target_state);
  2024. EXPORT_SYMBOL(pci_prepare_to_sleep);
  2025. EXPORT_SYMBOL(pci_back_from_sleep);
  2026. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);