pci.c 43 KB

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