pci.c 69 KB

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