pci.c 60 KB

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