pci.c 42 KB

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