pci.c 36 KB

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