pci.c 35 KB

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