pci.c 42 KB

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