pci.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /*
  2. * $Id: pci.c,v 1.91 1999/01/21 13:34:01 davem Exp $
  3. *
  4. * PCI Bus Services, see include/linux/pci.h for further explanation.
  5. *
  6. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  7. * David Mosberger-Tang
  8. *
  9. * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pm.h>
  16. #include <linux/module.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/string.h>
  19. #include <linux/log2.h>
  20. #include <asm/dma.h> /* isa_dma_bridge_buggy */
  21. #include "pci.h"
  22. unsigned int pci_pm_d3_delay = 10;
  23. #ifdef CONFIG_PCI_DOMAINS
  24. int pci_domains_supported = 1;
  25. #endif
  26. #define DEFAULT_CARDBUS_IO_SIZE (256)
  27. #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
  28. /* pci=cbmemsize=nnM,cbiosize=nn can override this */
  29. unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
  30. unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
  31. /**
  32. * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  33. * @bus: pointer to PCI bus structure to search
  34. *
  35. * Given a PCI bus, returns the highest PCI bus number present in the set
  36. * including the given PCI bus and its list of child PCI buses.
  37. */
  38. unsigned char pci_bus_max_busnr(struct pci_bus* bus)
  39. {
  40. struct list_head *tmp;
  41. unsigned char max, n;
  42. max = bus->subordinate;
  43. list_for_each(tmp, &bus->children) {
  44. n = pci_bus_max_busnr(pci_bus_b(tmp));
  45. if(n > max)
  46. max = n;
  47. }
  48. return max;
  49. }
  50. EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
  51. #if 0
  52. /**
  53. * pci_max_busnr - returns maximum PCI bus number
  54. *
  55. * Returns the highest PCI bus number present in the system global list of
  56. * PCI buses.
  57. */
  58. unsigned char __devinit
  59. pci_max_busnr(void)
  60. {
  61. struct pci_bus *bus = NULL;
  62. unsigned char max, n;
  63. max = 0;
  64. while ((bus = pci_find_next_bus(bus)) != NULL) {
  65. n = pci_bus_max_busnr(bus);
  66. if(n > max)
  67. max = n;
  68. }
  69. return max;
  70. }
  71. #endif /* 0 */
  72. #define PCI_FIND_CAP_TTL 48
  73. static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
  74. u8 pos, int cap, int *ttl)
  75. {
  76. u8 id;
  77. while ((*ttl)--) {
  78. pci_bus_read_config_byte(bus, devfn, pos, &pos);
  79. if (pos < 0x40)
  80. break;
  81. pos &= ~3;
  82. pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
  83. &id);
  84. if (id == 0xff)
  85. break;
  86. if (id == cap)
  87. return pos;
  88. pos += PCI_CAP_LIST_NEXT;
  89. }
  90. return 0;
  91. }
  92. static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
  93. u8 pos, int cap)
  94. {
  95. int ttl = PCI_FIND_CAP_TTL;
  96. return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  97. }
  98. int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
  99. {
  100. return __pci_find_next_cap(dev->bus, dev->devfn,
  101. pos + PCI_CAP_LIST_NEXT, cap);
  102. }
  103. EXPORT_SYMBOL_GPL(pci_find_next_capability);
  104. static int __pci_bus_find_cap_start(struct pci_bus *bus,
  105. unsigned int devfn, u8 hdr_type)
  106. {
  107. u16 status;
  108. pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
  109. if (!(status & PCI_STATUS_CAP_LIST))
  110. return 0;
  111. switch (hdr_type) {
  112. case PCI_HEADER_TYPE_NORMAL:
  113. case PCI_HEADER_TYPE_BRIDGE:
  114. return PCI_CAPABILITY_LIST;
  115. case PCI_HEADER_TYPE_CARDBUS:
  116. return PCI_CB_CAPABILITY_LIST;
  117. default:
  118. return 0;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * pci_find_capability - query for devices' capabilities
  124. * @dev: PCI device to query
  125. * @cap: capability code
  126. *
  127. * Tell if a device supports a given PCI capability.
  128. * Returns the address of the requested capability structure within the
  129. * device's PCI configuration space or 0 in case the device does not
  130. * support it. Possible values for @cap:
  131. *
  132. * %PCI_CAP_ID_PM Power Management
  133. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  134. * %PCI_CAP_ID_VPD Vital Product Data
  135. * %PCI_CAP_ID_SLOTID Slot Identification
  136. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  137. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  138. * %PCI_CAP_ID_PCIX PCI-X
  139. * %PCI_CAP_ID_EXP PCI Express
  140. */
  141. int pci_find_capability(struct pci_dev *dev, int cap)
  142. {
  143. int pos;
  144. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  145. if (pos)
  146. pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  147. return pos;
  148. }
  149. /**
  150. * pci_bus_find_capability - query for devices' capabilities
  151. * @bus: the PCI bus to query
  152. * @devfn: PCI device to query
  153. * @cap: capability code
  154. *
  155. * Like pci_find_capability() but works for pci devices that do not have a
  156. * pci_dev structure set up yet.
  157. *
  158. * Returns the address of the requested capability structure within the
  159. * device's PCI configuration space or 0 in case the device does not
  160. * support it.
  161. */
  162. int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
  163. {
  164. int pos;
  165. u8 hdr_type;
  166. pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  167. pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
  168. if (pos)
  169. pos = __pci_find_next_cap(bus, devfn, pos, cap);
  170. return pos;
  171. }
  172. /**
  173. * pci_find_ext_capability - Find an extended capability
  174. * @dev: PCI device to query
  175. * @cap: capability code
  176. *
  177. * Returns the address of the requested extended capability structure
  178. * within the device's PCI configuration space or 0 if the device does
  179. * not support it. Possible values for @cap:
  180. *
  181. * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
  182. * %PCI_EXT_CAP_ID_VC Virtual Channel
  183. * %PCI_EXT_CAP_ID_DSN Device Serial Number
  184. * %PCI_EXT_CAP_ID_PWR Power Budgeting
  185. */
  186. int pci_find_ext_capability(struct pci_dev *dev, int cap)
  187. {
  188. u32 header;
  189. int ttl = 480; /* 3840 bytes, minimum 8 bytes per capability */
  190. int pos = 0x100;
  191. if (dev->cfg_size <= 256)
  192. return 0;
  193. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  194. return 0;
  195. /*
  196. * If we have no capabilities, this is indicated by cap ID,
  197. * cap version and next pointer all being 0.
  198. */
  199. if (header == 0)
  200. return 0;
  201. while (ttl-- > 0) {
  202. if (PCI_EXT_CAP_ID(header) == cap)
  203. return pos;
  204. pos = PCI_EXT_CAP_NEXT(header);
  205. if (pos < 0x100)
  206. break;
  207. if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
  208. break;
  209. }
  210. return 0;
  211. }
  212. EXPORT_SYMBOL_GPL(pci_find_ext_capability);
  213. static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
  214. {
  215. int rc, ttl = PCI_FIND_CAP_TTL;
  216. u8 cap, mask;
  217. if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
  218. mask = HT_3BIT_CAP_MASK;
  219. else
  220. mask = HT_5BIT_CAP_MASK;
  221. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
  222. PCI_CAP_ID_HT, &ttl);
  223. while (pos) {
  224. rc = pci_read_config_byte(dev, pos + 3, &cap);
  225. if (rc != PCIBIOS_SUCCESSFUL)
  226. return 0;
  227. if ((cap & mask) == ht_cap)
  228. return pos;
  229. pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
  230. pos + PCI_CAP_LIST_NEXT,
  231. PCI_CAP_ID_HT, &ttl);
  232. }
  233. return 0;
  234. }
  235. /**
  236. * pci_find_next_ht_capability - query a device's Hypertransport capabilities
  237. * @dev: PCI device to query
  238. * @pos: Position from which to continue searching
  239. * @ht_cap: Hypertransport capability code
  240. *
  241. * To be used in conjunction with pci_find_ht_capability() to search for
  242. * all capabilities matching @ht_cap. @pos should always be a value returned
  243. * from pci_find_ht_capability().
  244. *
  245. * NB. To be 100% safe against broken PCI devices, the caller should take
  246. * steps to avoid an infinite loop.
  247. */
  248. int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
  249. {
  250. return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
  251. }
  252. EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
  253. /**
  254. * pci_find_ht_capability - query a device's Hypertransport capabilities
  255. * @dev: PCI device to query
  256. * @ht_cap: Hypertransport capability code
  257. *
  258. * Tell if a device supports a given Hypertransport capability.
  259. * Returns an address within the device's PCI configuration space
  260. * or 0 in case the device does not support the request capability.
  261. * The address points to the PCI capability, of type PCI_CAP_ID_HT,
  262. * which has a Hypertransport capability matching @ht_cap.
  263. */
  264. int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
  265. {
  266. int pos;
  267. pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  268. if (pos)
  269. pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
  270. return pos;
  271. }
  272. EXPORT_SYMBOL_GPL(pci_find_ht_capability);
  273. void pcie_wait_pending_transaction(struct pci_dev *dev)
  274. {
  275. int pos;
  276. u16 reg16;
  277. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  278. if (!pos)
  279. return;
  280. while (1) {
  281. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
  282. if (!(reg16 & PCI_EXP_DEVSTA_TRPND))
  283. break;
  284. cpu_relax();
  285. }
  286. }
  287. EXPORT_SYMBOL_GPL(pcie_wait_pending_transaction);
  288. /**
  289. * pci_find_parent_resource - return resource region of parent bus of given region
  290. * @dev: PCI device structure contains resources to be searched
  291. * @res: child resource record for which parent is sought
  292. *
  293. * For given resource region of given device, return the resource
  294. * region of parent bus the given region is contained in or where
  295. * it should be allocated from.
  296. */
  297. struct resource *
  298. pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
  299. {
  300. const struct pci_bus *bus = dev->bus;
  301. int i;
  302. struct resource *best = NULL;
  303. for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  304. struct resource *r = bus->resource[i];
  305. if (!r)
  306. continue;
  307. if (res->start && !(res->start >= r->start && res->end <= r->end))
  308. continue; /* Not contained */
  309. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  310. continue; /* Wrong type */
  311. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
  312. return r; /* Exact match */
  313. if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
  314. best = r; /* Approximating prefetchable by non-prefetchable */
  315. }
  316. return best;
  317. }
  318. /**
  319. * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
  320. * @dev: PCI device to have its BARs restored
  321. *
  322. * Restore the BAR values for a given device, so as to make it
  323. * accessible by its driver.
  324. */
  325. static void
  326. pci_restore_bars(struct pci_dev *dev)
  327. {
  328. int i, numres;
  329. switch (dev->hdr_type) {
  330. case PCI_HEADER_TYPE_NORMAL:
  331. numres = 6;
  332. break;
  333. case PCI_HEADER_TYPE_BRIDGE:
  334. numres = 2;
  335. break;
  336. case PCI_HEADER_TYPE_CARDBUS:
  337. numres = 1;
  338. break;
  339. default:
  340. /* Should never get here, but just in case... */
  341. return;
  342. }
  343. for (i = 0; i < numres; i ++)
  344. pci_update_resource(dev, &dev->resource[i], i);
  345. }
  346. int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
  347. /**
  348. * pci_set_power_state - Set the power state of a PCI device
  349. * @dev: PCI device to be suspended
  350. * @state: PCI power state (D0, D1, D2, D3hot, D3cold) we're entering
  351. *
  352. * Transition a device to a new power state, using the Power Management
  353. * Capabilities in the device's config space.
  354. *
  355. * RETURN VALUE:
  356. * -EINVAL if trying to enter a lower state than we're already in.
  357. * 0 if we're already in the requested state.
  358. * -EIO if device does not support PCI PM.
  359. * 0 if we can successfully change the power state.
  360. */
  361. int
  362. pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  363. {
  364. int pm, need_restore = 0;
  365. u16 pmcsr, pmc;
  366. /* bound the state we're entering */
  367. if (state > PCI_D3hot)
  368. state = PCI_D3hot;
  369. /*
  370. * If the device or the parent bridge can't support PCI PM, ignore
  371. * the request if we're doing anything besides putting it into D0
  372. * (which would only happen on boot).
  373. */
  374. if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  375. return 0;
  376. /* find PCI PM capability in list */
  377. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  378. /* abort if the device doesn't support PM capabilities */
  379. if (!pm)
  380. return -EIO;
  381. /* Validate current state:
  382. * Can enter D0 from any state, but if we can only go deeper
  383. * to sleep if we're already in a low power state
  384. */
  385. if (state != PCI_D0 && dev->current_state > state) {
  386. printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
  387. __FUNCTION__, pci_name(dev), state, dev->current_state);
  388. return -EINVAL;
  389. } else if (dev->current_state == state)
  390. return 0; /* we're already there */
  391. pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
  392. if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  393. printk(KERN_DEBUG
  394. "PCI: %s has unsupported PM cap regs version (%u)\n",
  395. pci_name(dev), pmc & PCI_PM_CAP_VER_MASK);
  396. return -EIO;
  397. }
  398. /* check if this device supports the desired state */
  399. if (state == PCI_D1 && !(pmc & PCI_PM_CAP_D1))
  400. return -EIO;
  401. else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
  402. return -EIO;
  403. pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  404. /* If we're (effectively) in D3, force entire word to 0.
  405. * This doesn't affect PME_Status, disables PME_En, and
  406. * sets PowerState to 0.
  407. */
  408. switch (dev->current_state) {
  409. case PCI_D0:
  410. case PCI_D1:
  411. case PCI_D2:
  412. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  413. pmcsr |= state;
  414. break;
  415. case PCI_UNKNOWN: /* Boot-up */
  416. if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  417. && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  418. need_restore = 1;
  419. /* Fall-through: force to D0 */
  420. default:
  421. pmcsr = 0;
  422. break;
  423. }
  424. /* enter specified state */
  425. pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  426. /* Mandatory power management transition delays */
  427. /* see PCI PM 1.1 5.6.1 table 18 */
  428. if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  429. msleep(pci_pm_d3_delay);
  430. else if (state == PCI_D2 || dev->current_state == PCI_D2)
  431. udelay(200);
  432. /*
  433. * Give firmware a chance to be called, such as ACPI _PRx, _PSx
  434. * Firmware method after native method ?
  435. */
  436. if (platform_pci_set_power_state)
  437. platform_pci_set_power_state(dev, state);
  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. return 0;
  454. }
  455. pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev, pm_message_t state);
  456. /**
  457. * pci_choose_state - Choose the power state of a PCI device
  458. * @dev: PCI device to be suspended
  459. * @state: target sleep state for the whole system. This is the value
  460. * that is passed to suspend() function.
  461. *
  462. * Returns PCI power state suitable for given device and given system
  463. * message.
  464. */
  465. pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
  466. {
  467. pci_power_t ret;
  468. if (!pci_find_capability(dev, PCI_CAP_ID_PM))
  469. return PCI_D0;
  470. if (platform_pci_choose_state) {
  471. ret = platform_pci_choose_state(dev, state);
  472. if (ret != PCI_POWER_ERROR)
  473. return ret;
  474. }
  475. switch (state.event) {
  476. case PM_EVENT_ON:
  477. return PCI_D0;
  478. case PM_EVENT_FREEZE:
  479. case PM_EVENT_PRETHAW:
  480. /* REVISIT both freeze and pre-thaw "should" use D0 */
  481. case PM_EVENT_SUSPEND:
  482. return PCI_D3hot;
  483. default:
  484. printk("Unrecognized suspend event %d\n", state.event);
  485. BUG();
  486. }
  487. return PCI_D0;
  488. }
  489. EXPORT_SYMBOL(pci_choose_state);
  490. static int pci_save_pcie_state(struct pci_dev *dev)
  491. {
  492. int pos, i = 0;
  493. struct pci_cap_saved_state *save_state;
  494. u16 *cap;
  495. int found = 0;
  496. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  497. if (pos <= 0)
  498. return 0;
  499. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  500. if (!save_state)
  501. save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL);
  502. else
  503. found = 1;
  504. if (!save_state) {
  505. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  506. return -ENOMEM;
  507. }
  508. cap = (u16 *)&save_state->data[0];
  509. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
  510. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
  511. pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
  512. pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
  513. save_state->cap_nr = PCI_CAP_ID_EXP;
  514. if (!found)
  515. pci_add_saved_cap(dev, save_state);
  516. return 0;
  517. }
  518. static void pci_restore_pcie_state(struct pci_dev *dev)
  519. {
  520. int i = 0, pos;
  521. struct pci_cap_saved_state *save_state;
  522. u16 *cap;
  523. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
  524. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  525. if (!save_state || pos <= 0)
  526. return;
  527. cap = (u16 *)&save_state->data[0];
  528. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
  529. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
  530. pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
  531. pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
  532. }
  533. static int pci_save_pcix_state(struct pci_dev *dev)
  534. {
  535. int pos, i = 0;
  536. struct pci_cap_saved_state *save_state;
  537. u16 *cap;
  538. int found = 0;
  539. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  540. if (pos <= 0)
  541. return 0;
  542. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  543. if (!save_state)
  544. save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL);
  545. else
  546. found = 1;
  547. if (!save_state) {
  548. dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n");
  549. return -ENOMEM;
  550. }
  551. cap = (u16 *)&save_state->data[0];
  552. pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]);
  553. save_state->cap_nr = PCI_CAP_ID_PCIX;
  554. if (!found)
  555. pci_add_saved_cap(dev, save_state);
  556. return 0;
  557. }
  558. static void pci_restore_pcix_state(struct pci_dev *dev)
  559. {
  560. int i = 0, pos;
  561. struct pci_cap_saved_state *save_state;
  562. u16 *cap;
  563. save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
  564. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  565. if (!save_state || pos <= 0)
  566. return;
  567. cap = (u16 *)&save_state->data[0];
  568. pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
  569. }
  570. /**
  571. * pci_save_state - save the PCI configuration space of a device before suspending
  572. * @dev: - PCI device that we're dealing with
  573. */
  574. int
  575. pci_save_state(struct pci_dev *dev)
  576. {
  577. int i;
  578. /* XXX: 100% dword access ok here? */
  579. for (i = 0; i < 16; i++)
  580. pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
  581. if ((i = pci_save_pcie_state(dev)) != 0)
  582. return i;
  583. if ((i = pci_save_pcix_state(dev)) != 0)
  584. return i;
  585. return 0;
  586. }
  587. /**
  588. * pci_restore_state - Restore the saved state of a PCI device
  589. * @dev: - PCI device that we're dealing with
  590. */
  591. int
  592. pci_restore_state(struct pci_dev *dev)
  593. {
  594. int i;
  595. u32 val;
  596. /* PCI Express register must be restored first */
  597. pci_restore_pcie_state(dev);
  598. /*
  599. * The Base Address register should be programmed before the command
  600. * register(s)
  601. */
  602. for (i = 15; i >= 0; i--) {
  603. pci_read_config_dword(dev, i * 4, &val);
  604. if (val != dev->saved_config_space[i]) {
  605. printk(KERN_DEBUG "PM: Writing back config space on "
  606. "device %s at offset %x (was %x, writing %x)\n",
  607. pci_name(dev), i,
  608. val, (int)dev->saved_config_space[i]);
  609. pci_write_config_dword(dev,i * 4,
  610. dev->saved_config_space[i]);
  611. }
  612. }
  613. pci_restore_pcix_state(dev);
  614. pci_restore_msi_state(dev);
  615. return 0;
  616. }
  617. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  618. {
  619. int err;
  620. err = pci_set_power_state(dev, PCI_D0);
  621. if (err < 0 && err != -EIO)
  622. return err;
  623. err = pcibios_enable_device(dev, bars);
  624. if (err < 0)
  625. return err;
  626. pci_fixup_device(pci_fixup_enable, dev);
  627. return 0;
  628. }
  629. /**
  630. * pci_reenable_device - Resume abandoned device
  631. * @dev: PCI device to be resumed
  632. *
  633. * Note this function is a backend of pci_default_resume and is not supposed
  634. * to be called by normal code, write proper resume handler and use it instead.
  635. */
  636. int pci_reenable_device(struct pci_dev *dev)
  637. {
  638. if (atomic_read(&dev->enable_cnt))
  639. return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
  640. return 0;
  641. }
  642. /**
  643. * pci_enable_device_bars - Initialize some of a device for use
  644. * @dev: PCI device to be initialized
  645. * @bars: bitmask of BAR's that must be configured
  646. *
  647. * Initialize device before it's used by a driver. Ask low-level code
  648. * to enable selected I/O and memory resources. Wake up the device if it
  649. * was suspended. Beware, this function can fail.
  650. */
  651. int
  652. pci_enable_device_bars(struct pci_dev *dev, int bars)
  653. {
  654. int err;
  655. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  656. return 0; /* already enabled */
  657. err = do_pci_enable_device(dev, bars);
  658. if (err < 0)
  659. atomic_dec(&dev->enable_cnt);
  660. return err;
  661. }
  662. static int __pci_enable_device_flags(struct pci_dev *dev,
  663. resource_size_t flags)
  664. {
  665. int err;
  666. int i, bars = 0;
  667. if (atomic_add_return(1, &dev->enable_cnt) > 1)
  668. return 0; /* already enabled */
  669. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  670. if (dev->resource[i].flags & flags)
  671. bars |= (1 << i);
  672. err = do_pci_enable_device(dev, bars);
  673. if (err < 0)
  674. atomic_dec(&dev->enable_cnt);
  675. return err;
  676. }
  677. /**
  678. * pci_enable_device_io - Initialize a device for use with IO space
  679. * @dev: PCI device to be initialized
  680. *
  681. * Initialize device before it's used by a driver. Ask low-level code
  682. * to enable I/O resources. Wake up the device if it was suspended.
  683. * Beware, this function can fail.
  684. */
  685. int pci_enable_device_io(struct pci_dev *dev)
  686. {
  687. return __pci_enable_device_flags(dev, IORESOURCE_IO);
  688. }
  689. /**
  690. * pci_enable_device_mem - Initialize a device for use with Memory space
  691. * @dev: PCI device to be initialized
  692. *
  693. * Initialize device before it's used by a driver. Ask low-level code
  694. * to enable Memory resources. Wake up the device if it was suspended.
  695. * Beware, this function can fail.
  696. */
  697. int pci_enable_device_mem(struct pci_dev *dev)
  698. {
  699. return __pci_enable_device_flags(dev, IORESOURCE_MEM);
  700. }
  701. /**
  702. * pci_enable_device - Initialize device before it's used by a driver.
  703. * @dev: PCI device to be initialized
  704. *
  705. * Initialize device before it's used by a driver. Ask low-level code
  706. * to enable I/O and memory. Wake up the device if it was suspended.
  707. * Beware, this function can fail.
  708. *
  709. * Note we don't actually enable the device many times if we call
  710. * this function repeatedly (we just increment the count).
  711. */
  712. int pci_enable_device(struct pci_dev *dev)
  713. {
  714. return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  715. }
  716. /*
  717. * Managed PCI resources. This manages device on/off, intx/msi/msix
  718. * on/off and BAR regions. pci_dev itself records msi/msix status, so
  719. * there's no need to track it separately. pci_devres is initialized
  720. * when a device is enabled using managed PCI device enable interface.
  721. */
  722. struct pci_devres {
  723. unsigned int enabled:1;
  724. unsigned int pinned:1;
  725. unsigned int orig_intx:1;
  726. unsigned int restore_intx:1;
  727. u32 region_mask;
  728. };
  729. static void pcim_release(struct device *gendev, void *res)
  730. {
  731. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  732. struct pci_devres *this = res;
  733. int i;
  734. if (dev->msi_enabled)
  735. pci_disable_msi(dev);
  736. if (dev->msix_enabled)
  737. pci_disable_msix(dev);
  738. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  739. if (this->region_mask & (1 << i))
  740. pci_release_region(dev, i);
  741. if (this->restore_intx)
  742. pci_intx(dev, this->orig_intx);
  743. if (this->enabled && !this->pinned)
  744. pci_disable_device(dev);
  745. }
  746. static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
  747. {
  748. struct pci_devres *dr, *new_dr;
  749. dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
  750. if (dr)
  751. return dr;
  752. new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
  753. if (!new_dr)
  754. return NULL;
  755. return devres_get(&pdev->dev, new_dr, NULL, NULL);
  756. }
  757. static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
  758. {
  759. if (pci_is_managed(pdev))
  760. return devres_find(&pdev->dev, pcim_release, NULL, NULL);
  761. return NULL;
  762. }
  763. /**
  764. * pcim_enable_device - Managed pci_enable_device()
  765. * @pdev: PCI device to be initialized
  766. *
  767. * Managed pci_enable_device().
  768. */
  769. int pcim_enable_device(struct pci_dev *pdev)
  770. {
  771. struct pci_devres *dr;
  772. int rc;
  773. dr = get_pci_dr(pdev);
  774. if (unlikely(!dr))
  775. return -ENOMEM;
  776. WARN_ON(!!dr->enabled);
  777. rc = pci_enable_device(pdev);
  778. if (!rc) {
  779. pdev->is_managed = 1;
  780. dr->enabled = 1;
  781. }
  782. return rc;
  783. }
  784. /**
  785. * pcim_pin_device - Pin managed PCI device
  786. * @pdev: PCI device to pin
  787. *
  788. * Pin managed PCI device @pdev. Pinned device won't be disabled on
  789. * driver detach. @pdev must have been enabled with
  790. * pcim_enable_device().
  791. */
  792. void pcim_pin_device(struct pci_dev *pdev)
  793. {
  794. struct pci_devres *dr;
  795. dr = find_pci_dr(pdev);
  796. WARN_ON(!dr || !dr->enabled);
  797. if (dr)
  798. dr->pinned = 1;
  799. }
  800. /**
  801. * pcibios_disable_device - disable arch specific PCI resources for device dev
  802. * @dev: the PCI device to disable
  803. *
  804. * Disables architecture specific PCI resources for the device. This
  805. * is the default implementation. Architecture implementations can
  806. * override this.
  807. */
  808. void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
  809. /**
  810. * pci_disable_device - Disable PCI device after use
  811. * @dev: PCI device to be disabled
  812. *
  813. * Signal to the system that the PCI device is not in use by the system
  814. * anymore. This only involves disabling PCI bus-mastering, if active.
  815. *
  816. * Note we don't actually disable the device until all callers of
  817. * pci_device_enable() have called pci_device_disable().
  818. */
  819. void
  820. pci_disable_device(struct pci_dev *dev)
  821. {
  822. struct pci_devres *dr;
  823. u16 pci_command;
  824. dr = find_pci_dr(dev);
  825. if (dr)
  826. dr->enabled = 0;
  827. if (atomic_sub_return(1, &dev->enable_cnt) != 0)
  828. return;
  829. /* Wait for all transactions are finished before disabling the device */
  830. pcie_wait_pending_transaction(dev);
  831. pci_read_config_word(dev, PCI_COMMAND, &pci_command);
  832. if (pci_command & PCI_COMMAND_MASTER) {
  833. pci_command &= ~PCI_COMMAND_MASTER;
  834. pci_write_config_word(dev, PCI_COMMAND, pci_command);
  835. }
  836. dev->is_busmaster = 0;
  837. pcibios_disable_device(dev);
  838. }
  839. /**
  840. * pcibios_set_pcie_reset_state - set reset state for device dev
  841. * @dev: the PCI-E device reset
  842. * @state: Reset state to enter into
  843. *
  844. *
  845. * Sets the PCI-E reset state for the device. This is the default
  846. * implementation. Architecture implementations can override this.
  847. */
  848. int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
  849. enum pcie_reset_state state)
  850. {
  851. return -EINVAL;
  852. }
  853. /**
  854. * pci_set_pcie_reset_state - set reset state for device dev
  855. * @dev: the PCI-E device reset
  856. * @state: Reset state to enter into
  857. *
  858. *
  859. * Sets the PCI reset state for the device.
  860. */
  861. int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  862. {
  863. return pcibios_set_pcie_reset_state(dev, state);
  864. }
  865. /**
  866. * pci_enable_wake - enable PCI device as wakeup event source
  867. * @dev: PCI device affected
  868. * @state: PCI state from which device will issue wakeup events
  869. * @enable: True to enable event generation; false to disable
  870. *
  871. * This enables the device as a wakeup event source, or disables it.
  872. * When such events involves platform-specific hooks, those hooks are
  873. * called automatically by this routine.
  874. *
  875. * Devices with legacy power management (no standard PCI PM capabilities)
  876. * always require such platform hooks. Depending on the platform, devices
  877. * supporting the standard PCI PME# signal may require such platform hooks;
  878. * they always update bits in config space to allow PME# generation.
  879. *
  880. * -EIO is returned if the device can't ever be a wakeup event source.
  881. * -EINVAL is returned if the device can't generate wakeup events from
  882. * the specified PCI state. Returns zero if the operation is successful.
  883. */
  884. int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
  885. {
  886. int pm;
  887. int status;
  888. u16 value;
  889. /* Note that drivers should verify device_may_wakeup(&dev->dev)
  890. * before calling this function. Platform code should report
  891. * errors when drivers try to enable wakeup on devices that
  892. * can't issue wakeups, or on which wakeups were disabled by
  893. * userspace updating the /sys/devices.../power/wakeup file.
  894. */
  895. status = call_platform_enable_wakeup(&dev->dev, enable);
  896. /* find PCI PM capability in list */
  897. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  898. /* If device doesn't support PM Capabilities, but caller wants to
  899. * disable wake events, it's a NOP. Otherwise fail unless the
  900. * platform hooks handled this legacy device already.
  901. */
  902. if (!pm)
  903. return enable ? status : 0;
  904. /* Check device's ability to generate PME# */
  905. pci_read_config_word(dev,pm+PCI_PM_PMC,&value);
  906. value &= PCI_PM_CAP_PME_MASK;
  907. value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */
  908. /* Check if it can generate PME# from requested state. */
  909. if (!value || !(value & (1 << state))) {
  910. /* if it can't, revert what the platform hook changed,
  911. * always reporting the base "EINVAL, can't PME#" error
  912. */
  913. if (enable)
  914. call_platform_enable_wakeup(&dev->dev, 0);
  915. return enable ? -EINVAL : 0;
  916. }
  917. pci_read_config_word(dev, pm + PCI_PM_CTRL, &value);
  918. /* Clear PME_Status by writing 1 to it and enable PME# */
  919. value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
  920. if (!enable)
  921. value &= ~PCI_PM_CTRL_PME_ENABLE;
  922. pci_write_config_word(dev, pm + PCI_PM_CTRL, value);
  923. return 0;
  924. }
  925. int
  926. pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  927. {
  928. u8 pin;
  929. pin = dev->pin;
  930. if (!pin)
  931. return -1;
  932. pin--;
  933. while (dev->bus->self) {
  934. pin = (pin + PCI_SLOT(dev->devfn)) % 4;
  935. dev = dev->bus->self;
  936. }
  937. *bridge = dev;
  938. return pin;
  939. }
  940. /**
  941. * pci_release_region - Release a PCI bar
  942. * @pdev: PCI device whose resources were previously reserved by pci_request_region
  943. * @bar: BAR to release
  944. *
  945. * Releases the PCI I/O and memory resources previously reserved by a
  946. * successful call to pci_request_region. Call this function only
  947. * after all use of the PCI regions has ceased.
  948. */
  949. void pci_release_region(struct pci_dev *pdev, int bar)
  950. {
  951. struct pci_devres *dr;
  952. if (pci_resource_len(pdev, bar) == 0)
  953. return;
  954. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  955. release_region(pci_resource_start(pdev, bar),
  956. pci_resource_len(pdev, bar));
  957. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  958. release_mem_region(pci_resource_start(pdev, bar),
  959. pci_resource_len(pdev, bar));
  960. dr = find_pci_dr(pdev);
  961. if (dr)
  962. dr->region_mask &= ~(1 << bar);
  963. }
  964. /**
  965. * pci_request_region - Reserved PCI I/O and memory resource
  966. * @pdev: PCI device whose resources are to be reserved
  967. * @bar: BAR to be reserved
  968. * @res_name: Name to be associated with resource.
  969. *
  970. * Mark the PCI region associated with PCI device @pdev BR @bar as
  971. * being reserved by owner @res_name. Do not access any
  972. * address inside the PCI regions unless this call returns
  973. * successfully.
  974. *
  975. * Returns 0 on success, or %EBUSY on error. A warning
  976. * message is also printed on failure.
  977. */
  978. int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
  979. {
  980. struct pci_devres *dr;
  981. if (pci_resource_len(pdev, bar) == 0)
  982. return 0;
  983. if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
  984. if (!request_region(pci_resource_start(pdev, bar),
  985. pci_resource_len(pdev, bar), res_name))
  986. goto err_out;
  987. }
  988. else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  989. if (!request_mem_region(pci_resource_start(pdev, bar),
  990. pci_resource_len(pdev, bar), res_name))
  991. goto err_out;
  992. }
  993. dr = find_pci_dr(pdev);
  994. if (dr)
  995. dr->region_mask |= 1 << bar;
  996. return 0;
  997. err_out:
  998. printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%llx@%llx "
  999. "for device %s\n",
  1000. pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
  1001. bar + 1, /* PCI BAR # */
  1002. (unsigned long long)pci_resource_len(pdev, bar),
  1003. (unsigned long long)pci_resource_start(pdev, bar),
  1004. pci_name(pdev));
  1005. return -EBUSY;
  1006. }
  1007. /**
  1008. * pci_release_selected_regions - Release selected PCI I/O and memory resources
  1009. * @pdev: PCI device whose resources were previously reserved
  1010. * @bars: Bitmask of BARs to be released
  1011. *
  1012. * Release selected PCI I/O and memory resources previously reserved.
  1013. * Call this function only after all use of the PCI regions has ceased.
  1014. */
  1015. void pci_release_selected_regions(struct pci_dev *pdev, int bars)
  1016. {
  1017. int i;
  1018. for (i = 0; i < 6; i++)
  1019. if (bars & (1 << i))
  1020. pci_release_region(pdev, i);
  1021. }
  1022. /**
  1023. * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
  1024. * @pdev: PCI device whose resources are to be reserved
  1025. * @bars: Bitmask of BARs to be requested
  1026. * @res_name: Name to be associated with resource
  1027. */
  1028. int pci_request_selected_regions(struct pci_dev *pdev, int bars,
  1029. const char *res_name)
  1030. {
  1031. int i;
  1032. for (i = 0; i < 6; i++)
  1033. if (bars & (1 << i))
  1034. if(pci_request_region(pdev, i, res_name))
  1035. goto err_out;
  1036. return 0;
  1037. err_out:
  1038. while(--i >= 0)
  1039. if (bars & (1 << i))
  1040. pci_release_region(pdev, i);
  1041. return -EBUSY;
  1042. }
  1043. /**
  1044. * pci_release_regions - Release reserved PCI I/O and memory resources
  1045. * @pdev: PCI device whose resources were previously reserved by pci_request_regions
  1046. *
  1047. * Releases all PCI I/O and memory resources previously reserved by a
  1048. * successful call to pci_request_regions. Call this function only
  1049. * after all use of the PCI regions has ceased.
  1050. */
  1051. void pci_release_regions(struct pci_dev *pdev)
  1052. {
  1053. pci_release_selected_regions(pdev, (1 << 6) - 1);
  1054. }
  1055. /**
  1056. * pci_request_regions - Reserved PCI I/O and memory resources
  1057. * @pdev: PCI device whose resources are to be reserved
  1058. * @res_name: Name to be associated with resource.
  1059. *
  1060. * Mark all PCI regions associated with PCI device @pdev as
  1061. * being reserved by owner @res_name. Do not access any
  1062. * address inside the PCI regions unless this call returns
  1063. * successfully.
  1064. *
  1065. * Returns 0 on success, or %EBUSY on error. A warning
  1066. * message is also printed on failure.
  1067. */
  1068. int pci_request_regions(struct pci_dev *pdev, const char *res_name)
  1069. {
  1070. return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
  1071. }
  1072. /**
  1073. * pci_set_master - enables bus-mastering for device dev
  1074. * @dev: the PCI device to enable
  1075. *
  1076. * Enables bus-mastering on the device and calls pcibios_set_master()
  1077. * to do the needed arch specific settings.
  1078. */
  1079. void
  1080. pci_set_master(struct pci_dev *dev)
  1081. {
  1082. u16 cmd;
  1083. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1084. if (! (cmd & PCI_COMMAND_MASTER)) {
  1085. pr_debug("PCI: Enabling bus mastering for device %s\n", pci_name(dev));
  1086. cmd |= PCI_COMMAND_MASTER;
  1087. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1088. }
  1089. dev->is_busmaster = 1;
  1090. pcibios_set_master(dev);
  1091. }
  1092. #ifdef PCI_DISABLE_MWI
  1093. int pci_set_mwi(struct pci_dev *dev)
  1094. {
  1095. return 0;
  1096. }
  1097. int pci_try_set_mwi(struct pci_dev *dev)
  1098. {
  1099. return 0;
  1100. }
  1101. void pci_clear_mwi(struct pci_dev *dev)
  1102. {
  1103. }
  1104. #else
  1105. #ifndef PCI_CACHE_LINE_BYTES
  1106. #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
  1107. #endif
  1108. /* This can be overridden by arch code. */
  1109. /* Don't forget this is measured in 32-bit words, not bytes */
  1110. u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
  1111. /**
  1112. * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
  1113. * @dev: the PCI device for which MWI is to be enabled
  1114. *
  1115. * Helper function for pci_set_mwi.
  1116. * Originally copied from drivers/net/acenic.c.
  1117. * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
  1118. *
  1119. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1120. */
  1121. static int
  1122. pci_set_cacheline_size(struct pci_dev *dev)
  1123. {
  1124. u8 cacheline_size;
  1125. if (!pci_cache_line_size)
  1126. return -EINVAL; /* The system doesn't support MWI. */
  1127. /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
  1128. equal to or multiple of the right value. */
  1129. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1130. if (cacheline_size >= pci_cache_line_size &&
  1131. (cacheline_size % pci_cache_line_size) == 0)
  1132. return 0;
  1133. /* Write the correct value. */
  1134. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
  1135. /* Read it back. */
  1136. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
  1137. if (cacheline_size == pci_cache_line_size)
  1138. return 0;
  1139. printk(KERN_DEBUG "PCI: cache line size of %d is not supported "
  1140. "by device %s\n", pci_cache_line_size << 2, pci_name(dev));
  1141. return -EINVAL;
  1142. }
  1143. /**
  1144. * pci_set_mwi - enables memory-write-invalidate PCI transaction
  1145. * @dev: the PCI device for which MWI is enabled
  1146. *
  1147. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1148. *
  1149. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1150. */
  1151. int
  1152. pci_set_mwi(struct pci_dev *dev)
  1153. {
  1154. int rc;
  1155. u16 cmd;
  1156. rc = pci_set_cacheline_size(dev);
  1157. if (rc)
  1158. return rc;
  1159. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1160. if (! (cmd & PCI_COMMAND_INVALIDATE)) {
  1161. pr_debug("PCI: Enabling Mem-Wr-Inval for device %s\n",
  1162. pci_name(dev));
  1163. cmd |= PCI_COMMAND_INVALIDATE;
  1164. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1165. }
  1166. return 0;
  1167. }
  1168. /**
  1169. * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
  1170. * @dev: the PCI device for which MWI is enabled
  1171. *
  1172. * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
  1173. * Callers are not required to check the return value.
  1174. *
  1175. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1176. */
  1177. int pci_try_set_mwi(struct pci_dev *dev)
  1178. {
  1179. int rc = pci_set_mwi(dev);
  1180. return rc;
  1181. }
  1182. /**
  1183. * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
  1184. * @dev: the PCI device to disable
  1185. *
  1186. * Disables PCI Memory-Write-Invalidate transaction on the device
  1187. */
  1188. void
  1189. pci_clear_mwi(struct pci_dev *dev)
  1190. {
  1191. u16 cmd;
  1192. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1193. if (cmd & PCI_COMMAND_INVALIDATE) {
  1194. cmd &= ~PCI_COMMAND_INVALIDATE;
  1195. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1196. }
  1197. }
  1198. #endif /* ! PCI_DISABLE_MWI */
  1199. /**
  1200. * pci_intx - enables/disables PCI INTx for device dev
  1201. * @pdev: the PCI device to operate on
  1202. * @enable: boolean: whether to enable or disable PCI INTx
  1203. *
  1204. * Enables/disables PCI INTx for device dev
  1205. */
  1206. void
  1207. pci_intx(struct pci_dev *pdev, int enable)
  1208. {
  1209. u16 pci_command, new;
  1210. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  1211. if (enable) {
  1212. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  1213. } else {
  1214. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  1215. }
  1216. if (new != pci_command) {
  1217. struct pci_devres *dr;
  1218. pci_write_config_word(pdev, PCI_COMMAND, new);
  1219. dr = find_pci_dr(pdev);
  1220. if (dr && !dr->restore_intx) {
  1221. dr->restore_intx = 1;
  1222. dr->orig_intx = !enable;
  1223. }
  1224. }
  1225. }
  1226. /**
  1227. * pci_msi_off - disables any msi or msix capabilities
  1228. * @dev: the PCI device to operate on
  1229. *
  1230. * If you want to use msi see pci_enable_msi and friends.
  1231. * This is a lower level primitive that allows us to disable
  1232. * msi operation at the device level.
  1233. */
  1234. void pci_msi_off(struct pci_dev *dev)
  1235. {
  1236. int pos;
  1237. u16 control;
  1238. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1239. if (pos) {
  1240. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  1241. control &= ~PCI_MSI_FLAGS_ENABLE;
  1242. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  1243. }
  1244. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1245. if (pos) {
  1246. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  1247. control &= ~PCI_MSIX_FLAGS_ENABLE;
  1248. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  1249. }
  1250. }
  1251. #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
  1252. /*
  1253. * These can be overridden by arch-specific implementations
  1254. */
  1255. int
  1256. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  1257. {
  1258. if (!pci_dma_supported(dev, mask))
  1259. return -EIO;
  1260. dev->dma_mask = mask;
  1261. return 0;
  1262. }
  1263. int
  1264. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  1265. {
  1266. if (!pci_dma_supported(dev, mask))
  1267. return -EIO;
  1268. dev->dev.coherent_dma_mask = mask;
  1269. return 0;
  1270. }
  1271. #endif
  1272. /**
  1273. * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
  1274. * @dev: PCI device to query
  1275. *
  1276. * Returns mmrbc: maximum designed memory read count in bytes
  1277. * or appropriate error value.
  1278. */
  1279. int pcix_get_max_mmrbc(struct pci_dev *dev)
  1280. {
  1281. int err, cap;
  1282. u32 stat;
  1283. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1284. if (!cap)
  1285. return -EINVAL;
  1286. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1287. if (err)
  1288. return -EINVAL;
  1289. return (stat & PCI_X_STATUS_MAX_READ) >> 12;
  1290. }
  1291. EXPORT_SYMBOL(pcix_get_max_mmrbc);
  1292. /**
  1293. * pcix_get_mmrbc - get PCI-X maximum memory read byte count
  1294. * @dev: PCI device to query
  1295. *
  1296. * Returns mmrbc: maximum memory read count in bytes
  1297. * or appropriate error value.
  1298. */
  1299. int pcix_get_mmrbc(struct pci_dev *dev)
  1300. {
  1301. int ret, cap;
  1302. u32 cmd;
  1303. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1304. if (!cap)
  1305. return -EINVAL;
  1306. ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1307. if (!ret)
  1308. ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
  1309. return ret;
  1310. }
  1311. EXPORT_SYMBOL(pcix_get_mmrbc);
  1312. /**
  1313. * pcix_set_mmrbc - set PCI-X maximum memory read byte count
  1314. * @dev: PCI device to query
  1315. * @mmrbc: maximum memory read count in bytes
  1316. * valid values are 512, 1024, 2048, 4096
  1317. *
  1318. * If possible sets maximum memory read byte count, some bridges have erratas
  1319. * that prevent this.
  1320. */
  1321. int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
  1322. {
  1323. int cap, err = -EINVAL;
  1324. u32 stat, cmd, v, o;
  1325. if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
  1326. goto out;
  1327. v = ffs(mmrbc) - 10;
  1328. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1329. if (!cap)
  1330. goto out;
  1331. err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
  1332. if (err)
  1333. goto out;
  1334. if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
  1335. return -E2BIG;
  1336. err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
  1337. if (err)
  1338. goto out;
  1339. o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
  1340. if (o != v) {
  1341. if (v > o && dev->bus &&
  1342. (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
  1343. return -EIO;
  1344. cmd &= ~PCI_X_CMD_MAX_READ;
  1345. cmd |= v << 2;
  1346. err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
  1347. }
  1348. out:
  1349. return err;
  1350. }
  1351. EXPORT_SYMBOL(pcix_set_mmrbc);
  1352. /**
  1353. * pcie_get_readrq - get PCI Express read request size
  1354. * @dev: PCI device to query
  1355. *
  1356. * Returns maximum memory read request in bytes
  1357. * or appropriate error value.
  1358. */
  1359. int pcie_get_readrq(struct pci_dev *dev)
  1360. {
  1361. int ret, cap;
  1362. u16 ctl;
  1363. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1364. if (!cap)
  1365. return -EINVAL;
  1366. ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1367. if (!ret)
  1368. ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1369. return ret;
  1370. }
  1371. EXPORT_SYMBOL(pcie_get_readrq);
  1372. /**
  1373. * pcie_set_readrq - set PCI Express maximum memory read request
  1374. * @dev: PCI device to query
  1375. * @rq: maximum memory read count in bytes
  1376. * valid values are 128, 256, 512, 1024, 2048, 4096
  1377. *
  1378. * If possible sets maximum read byte count
  1379. */
  1380. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1381. {
  1382. int cap, err = -EINVAL;
  1383. u16 ctl, v;
  1384. if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1385. goto out;
  1386. v = (ffs(rq) - 8) << 12;
  1387. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  1388. if (!cap)
  1389. goto out;
  1390. err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
  1391. if (err)
  1392. goto out;
  1393. if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
  1394. ctl &= ~PCI_EXP_DEVCTL_READRQ;
  1395. ctl |= v;
  1396. err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
  1397. }
  1398. out:
  1399. return err;
  1400. }
  1401. EXPORT_SYMBOL(pcie_set_readrq);
  1402. /**
  1403. * pci_select_bars - Make BAR mask from the type of resource
  1404. * @dev: the PCI device for which BAR mask is made
  1405. * @flags: resource type mask to be selected
  1406. *
  1407. * This helper routine makes bar mask from the type of resource.
  1408. */
  1409. int pci_select_bars(struct pci_dev *dev, unsigned long flags)
  1410. {
  1411. int i, bars = 0;
  1412. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1413. if (pci_resource_flags(dev, i) & flags)
  1414. bars |= (1 << i);
  1415. return bars;
  1416. }
  1417. static void __devinit pci_no_domains(void)
  1418. {
  1419. #ifdef CONFIG_PCI_DOMAINS
  1420. pci_domains_supported = 0;
  1421. #endif
  1422. }
  1423. static int __devinit pci_init(void)
  1424. {
  1425. struct pci_dev *dev = NULL;
  1426. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1427. pci_fixup_device(pci_fixup_final, dev);
  1428. }
  1429. return 0;
  1430. }
  1431. static int __devinit pci_setup(char *str)
  1432. {
  1433. while (str) {
  1434. char *k = strchr(str, ',');
  1435. if (k)
  1436. *k++ = 0;
  1437. if (*str && (str = pcibios_setup(str)) && *str) {
  1438. if (!strcmp(str, "nomsi")) {
  1439. pci_no_msi();
  1440. } else if (!strcmp(str, "noaer")) {
  1441. pci_no_aer();
  1442. } else if (!strcmp(str, "nodomains")) {
  1443. pci_no_domains();
  1444. } else if (!strncmp(str, "cbiosize=", 9)) {
  1445. pci_cardbus_io_size = memparse(str + 9, &str);
  1446. } else if (!strncmp(str, "cbmemsize=", 10)) {
  1447. pci_cardbus_mem_size = memparse(str + 10, &str);
  1448. } else {
  1449. printk(KERN_ERR "PCI: Unknown option `%s'\n",
  1450. str);
  1451. }
  1452. }
  1453. str = k;
  1454. }
  1455. return 0;
  1456. }
  1457. early_param("pci", pci_setup);
  1458. device_initcall(pci_init);
  1459. EXPORT_SYMBOL(pci_reenable_device);
  1460. EXPORT_SYMBOL(pci_enable_device_bars);
  1461. EXPORT_SYMBOL(pci_enable_device_io);
  1462. EXPORT_SYMBOL(pci_enable_device_mem);
  1463. EXPORT_SYMBOL(pci_enable_device);
  1464. EXPORT_SYMBOL(pcim_enable_device);
  1465. EXPORT_SYMBOL(pcim_pin_device);
  1466. EXPORT_SYMBOL(pci_disable_device);
  1467. EXPORT_SYMBOL(pci_find_capability);
  1468. EXPORT_SYMBOL(pci_bus_find_capability);
  1469. EXPORT_SYMBOL(pci_release_regions);
  1470. EXPORT_SYMBOL(pci_request_regions);
  1471. EXPORT_SYMBOL(pci_release_region);
  1472. EXPORT_SYMBOL(pci_request_region);
  1473. EXPORT_SYMBOL(pci_release_selected_regions);
  1474. EXPORT_SYMBOL(pci_request_selected_regions);
  1475. EXPORT_SYMBOL(pci_set_master);
  1476. EXPORT_SYMBOL(pci_set_mwi);
  1477. EXPORT_SYMBOL(pci_try_set_mwi);
  1478. EXPORT_SYMBOL(pci_clear_mwi);
  1479. EXPORT_SYMBOL_GPL(pci_intx);
  1480. EXPORT_SYMBOL(pci_set_dma_mask);
  1481. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  1482. EXPORT_SYMBOL(pci_assign_resource);
  1483. EXPORT_SYMBOL(pci_find_parent_resource);
  1484. EXPORT_SYMBOL(pci_select_bars);
  1485. EXPORT_SYMBOL(pci_set_power_state);
  1486. EXPORT_SYMBOL(pci_save_state);
  1487. EXPORT_SYMBOL(pci_restore_state);
  1488. EXPORT_SYMBOL(pci_enable_wake);
  1489. EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);