pci.c 43 KB

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