pci.c 44 KB

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