probe.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. /*
  2. * probe.c - PCI detection and setup code
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/of_pci.h>
  9. #include <linux/pci_hotplug.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/pci-aspm.h>
  14. #include <asm-generic/pci-bridge.h>
  15. #include "pci.h"
  16. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  17. #define CARDBUS_RESERVE_BUSNR 3
  18. static struct resource busn_resource = {
  19. .name = "PCI busn",
  20. .start = 0,
  21. .end = 255,
  22. .flags = IORESOURCE_BUS,
  23. };
  24. /* Ugh. Need to stop exporting this to modules. */
  25. LIST_HEAD(pci_root_buses);
  26. EXPORT_SYMBOL(pci_root_buses);
  27. static LIST_HEAD(pci_domain_busn_res_list);
  28. struct pci_domain_busn_res {
  29. struct list_head list;
  30. struct resource res;
  31. int domain_nr;
  32. };
  33. static struct resource *get_pci_domain_busn_res(int domain_nr)
  34. {
  35. struct pci_domain_busn_res *r;
  36. list_for_each_entry(r, &pci_domain_busn_res_list, list)
  37. if (r->domain_nr == domain_nr)
  38. return &r->res;
  39. r = kzalloc(sizeof(*r), GFP_KERNEL);
  40. if (!r)
  41. return NULL;
  42. r->domain_nr = domain_nr;
  43. r->res.start = 0;
  44. r->res.end = 0xff;
  45. r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
  46. list_add_tail(&r->list, &pci_domain_busn_res_list);
  47. return &r->res;
  48. }
  49. static int find_anything(struct device *dev, void *data)
  50. {
  51. return 1;
  52. }
  53. /*
  54. * Some device drivers need know if pci is initiated.
  55. * Basically, we think pci is not initiated when there
  56. * is no device to be found on the pci_bus_type.
  57. */
  58. int no_pci_devices(void)
  59. {
  60. struct device *dev;
  61. int no_devices;
  62. dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
  63. no_devices = (dev == NULL);
  64. put_device(dev);
  65. return no_devices;
  66. }
  67. EXPORT_SYMBOL(no_pci_devices);
  68. /*
  69. * PCI Bus Class
  70. */
  71. static void release_pcibus_dev(struct device *dev)
  72. {
  73. struct pci_bus *pci_bus = to_pci_bus(dev);
  74. put_device(pci_bus->bridge);
  75. pci_bus_remove_resources(pci_bus);
  76. pci_release_bus_of_node(pci_bus);
  77. kfree(pci_bus);
  78. }
  79. static struct class pcibus_class = {
  80. .name = "pci_bus",
  81. .dev_release = &release_pcibus_dev,
  82. .dev_groups = pcibus_groups,
  83. };
  84. static int __init pcibus_class_init(void)
  85. {
  86. return class_register(&pcibus_class);
  87. }
  88. postcore_initcall(pcibus_class_init);
  89. static u64 pci_size(u64 base, u64 maxbase, u64 mask)
  90. {
  91. u64 size = mask & maxbase; /* Find the significant bits */
  92. if (!size)
  93. return 0;
  94. /* Get the lowest of them to find the decode size, and
  95. from that the extent. */
  96. size = (size & ~(size-1)) - 1;
  97. /* base == maxbase can be valid only if the BAR has
  98. already been programmed with all 1s. */
  99. if (base == maxbase && ((base | size) & mask) != mask)
  100. return 0;
  101. return size;
  102. }
  103. static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
  104. {
  105. u32 mem_type;
  106. unsigned long flags;
  107. if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  108. flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
  109. flags |= IORESOURCE_IO;
  110. return flags;
  111. }
  112. flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
  113. flags |= IORESOURCE_MEM;
  114. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  115. flags |= IORESOURCE_PREFETCH;
  116. mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  117. switch (mem_type) {
  118. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  119. break;
  120. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  121. /* 1M mem BAR treated as 32-bit BAR */
  122. break;
  123. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  124. flags |= IORESOURCE_MEM_64;
  125. break;
  126. default:
  127. /* mem unknown type treated as 32-bit BAR */
  128. break;
  129. }
  130. return flags;
  131. }
  132. #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
  133. /**
  134. * pci_read_base - read a PCI BAR
  135. * @dev: the PCI device
  136. * @type: type of the BAR
  137. * @res: resource buffer to be filled in
  138. * @pos: BAR position in the config space
  139. *
  140. * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
  141. */
  142. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  143. struct resource *res, unsigned int pos)
  144. {
  145. u32 l, sz, mask;
  146. u64 l64, sz64, mask64;
  147. u16 orig_cmd;
  148. struct pci_bus_region region, inverted_region;
  149. mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
  150. /* No printks while decoding is disabled! */
  151. if (!dev->mmio_always_on) {
  152. pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
  153. if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
  154. pci_write_config_word(dev, PCI_COMMAND,
  155. orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
  156. }
  157. }
  158. res->name = pci_name(dev);
  159. pci_read_config_dword(dev, pos, &l);
  160. pci_write_config_dword(dev, pos, l | mask);
  161. pci_read_config_dword(dev, pos, &sz);
  162. pci_write_config_dword(dev, pos, l);
  163. /*
  164. * All bits set in sz means the device isn't working properly.
  165. * If the BAR isn't implemented, all bits must be 0. If it's a
  166. * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
  167. * 1 must be clear.
  168. */
  169. if (sz == 0xffffffff)
  170. sz = 0;
  171. /*
  172. * I don't know how l can have all bits set. Copied from old code.
  173. * Maybe it fixes a bug on some ancient platform.
  174. */
  175. if (l == 0xffffffff)
  176. l = 0;
  177. if (type == pci_bar_unknown) {
  178. res->flags = decode_bar(dev, l);
  179. res->flags |= IORESOURCE_SIZEALIGN;
  180. if (res->flags & IORESOURCE_IO) {
  181. l64 = l & PCI_BASE_ADDRESS_IO_MASK;
  182. sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
  183. mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
  184. } else {
  185. l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
  186. sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
  187. mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  188. }
  189. } else {
  190. res->flags |= (l & IORESOURCE_ROM_ENABLE);
  191. l64 = l & PCI_ROM_ADDRESS_MASK;
  192. sz64 = sz & PCI_ROM_ADDRESS_MASK;
  193. mask64 = (u32)PCI_ROM_ADDRESS_MASK;
  194. }
  195. if (res->flags & IORESOURCE_MEM_64) {
  196. pci_read_config_dword(dev, pos + 4, &l);
  197. pci_write_config_dword(dev, pos + 4, ~0);
  198. pci_read_config_dword(dev, pos + 4, &sz);
  199. pci_write_config_dword(dev, pos + 4, l);
  200. l64 |= ((u64)l << 32);
  201. sz64 |= ((u64)sz << 32);
  202. mask64 |= ((u64)~0 << 32);
  203. }
  204. if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
  205. pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
  206. if (!sz64)
  207. goto fail;
  208. sz64 = pci_size(l64, sz64, mask64);
  209. if (!sz64) {
  210. dev_info(&dev->dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
  211. pos);
  212. goto fail;
  213. }
  214. if (res->flags & IORESOURCE_MEM_64) {
  215. if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
  216. && sz64 > 0x100000000ULL) {
  217. res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  218. res->start = 0;
  219. res->end = 0;
  220. dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
  221. pos, (unsigned long long)sz64);
  222. goto out;
  223. }
  224. if ((sizeof(pci_bus_addr_t) < 8) && l) {
  225. /* Above 32-bit boundary; try to reallocate */
  226. res->flags |= IORESOURCE_UNSET;
  227. res->start = 0;
  228. res->end = sz64;
  229. dev_info(&dev->dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
  230. pos, (unsigned long long)l64);
  231. goto out;
  232. }
  233. }
  234. region.start = l64;
  235. region.end = l64 + sz64;
  236. pcibios_bus_to_resource(dev->bus, res, &region);
  237. pcibios_resource_to_bus(dev->bus, &inverted_region, res);
  238. /*
  239. * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
  240. * the corresponding resource address (the physical address used by
  241. * the CPU. Converting that resource address back to a bus address
  242. * should yield the original BAR value:
  243. *
  244. * resource_to_bus(bus_to_resource(A)) == A
  245. *
  246. * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
  247. * be claimed by the device.
  248. */
  249. if (inverted_region.start != region.start) {
  250. res->flags |= IORESOURCE_UNSET;
  251. res->start = 0;
  252. res->end = region.end - region.start;
  253. dev_info(&dev->dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
  254. pos, (unsigned long long)region.start);
  255. }
  256. goto out;
  257. fail:
  258. res->flags = 0;
  259. out:
  260. if (res->flags)
  261. dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);
  262. return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  263. }
  264. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  265. {
  266. unsigned int pos, reg;
  267. for (pos = 0; pos < howmany; pos++) {
  268. struct resource *res = &dev->resource[pos];
  269. reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  270. pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
  271. }
  272. if (rom) {
  273. struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
  274. dev->rom_base_reg = rom;
  275. res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
  276. IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
  277. __pci_read_base(dev, pci_bar_mem32, res, rom);
  278. }
  279. }
  280. static void pci_read_bridge_io(struct pci_bus *child)
  281. {
  282. struct pci_dev *dev = child->self;
  283. u8 io_base_lo, io_limit_lo;
  284. unsigned long io_mask, io_granularity, base, limit;
  285. struct pci_bus_region region;
  286. struct resource *res;
  287. io_mask = PCI_IO_RANGE_MASK;
  288. io_granularity = 0x1000;
  289. if (dev->io_window_1k) {
  290. /* Support 1K I/O space granularity */
  291. io_mask = PCI_IO_1K_RANGE_MASK;
  292. io_granularity = 0x400;
  293. }
  294. res = child->resource[0];
  295. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  296. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  297. base = (io_base_lo & io_mask) << 8;
  298. limit = (io_limit_lo & io_mask) << 8;
  299. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  300. u16 io_base_hi, io_limit_hi;
  301. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  302. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  303. base |= ((unsigned long) io_base_hi << 16);
  304. limit |= ((unsigned long) io_limit_hi << 16);
  305. }
  306. if (base <= limit) {
  307. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  308. region.start = base;
  309. region.end = limit + io_granularity - 1;
  310. pcibios_bus_to_resource(dev->bus, res, &region);
  311. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  312. }
  313. }
  314. static void pci_read_bridge_mmio(struct pci_bus *child)
  315. {
  316. struct pci_dev *dev = child->self;
  317. u16 mem_base_lo, mem_limit_lo;
  318. unsigned long base, limit;
  319. struct pci_bus_region region;
  320. struct resource *res;
  321. res = child->resource[1];
  322. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  323. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  324. base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  325. limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  326. if (base <= limit) {
  327. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  328. region.start = base;
  329. region.end = limit + 0xfffff;
  330. pcibios_bus_to_resource(dev->bus, res, &region);
  331. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  332. }
  333. }
  334. static void pci_read_bridge_mmio_pref(struct pci_bus *child)
  335. {
  336. struct pci_dev *dev = child->self;
  337. u16 mem_base_lo, mem_limit_lo;
  338. u64 base64, limit64;
  339. pci_bus_addr_t base, limit;
  340. struct pci_bus_region region;
  341. struct resource *res;
  342. res = child->resource[2];
  343. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  344. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  345. base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  346. limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  347. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  348. u32 mem_base_hi, mem_limit_hi;
  349. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  350. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  351. /*
  352. * Some bridges set the base > limit by default, and some
  353. * (broken) BIOSes do not initialize them. If we find
  354. * this, just assume they are not being used.
  355. */
  356. if (mem_base_hi <= mem_limit_hi) {
  357. base64 |= (u64) mem_base_hi << 32;
  358. limit64 |= (u64) mem_limit_hi << 32;
  359. }
  360. }
  361. base = (pci_bus_addr_t) base64;
  362. limit = (pci_bus_addr_t) limit64;
  363. if (base != base64) {
  364. dev_err(&dev->dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
  365. (unsigned long long) base64);
  366. return;
  367. }
  368. if (base <= limit) {
  369. res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
  370. IORESOURCE_MEM | IORESOURCE_PREFETCH;
  371. if (res->flags & PCI_PREF_RANGE_TYPE_64)
  372. res->flags |= IORESOURCE_MEM_64;
  373. region.start = base;
  374. region.end = limit + 0xfffff;
  375. pcibios_bus_to_resource(dev->bus, res, &region);
  376. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  377. }
  378. }
  379. void pci_read_bridge_bases(struct pci_bus *child)
  380. {
  381. struct pci_dev *dev = child->self;
  382. struct resource *res;
  383. int i;
  384. if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
  385. return;
  386. dev_info(&dev->dev, "PCI bridge to %pR%s\n",
  387. &child->busn_res,
  388. dev->transparent ? " (subtractive decode)" : "");
  389. pci_bus_remove_resources(child);
  390. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  391. child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  392. pci_read_bridge_io(child);
  393. pci_read_bridge_mmio(child);
  394. pci_read_bridge_mmio_pref(child);
  395. if (dev->transparent) {
  396. pci_bus_for_each_resource(child->parent, res, i) {
  397. if (res && res->flags) {
  398. pci_bus_add_resource(child, res,
  399. PCI_SUBTRACTIVE_DECODE);
  400. dev_printk(KERN_DEBUG, &dev->dev,
  401. " bridge window %pR (subtractive decode)\n",
  402. res);
  403. }
  404. }
  405. }
  406. }
  407. static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
  408. {
  409. struct pci_bus *b;
  410. b = kzalloc(sizeof(*b), GFP_KERNEL);
  411. if (!b)
  412. return NULL;
  413. INIT_LIST_HEAD(&b->node);
  414. INIT_LIST_HEAD(&b->children);
  415. INIT_LIST_HEAD(&b->devices);
  416. INIT_LIST_HEAD(&b->slots);
  417. INIT_LIST_HEAD(&b->resources);
  418. b->max_bus_speed = PCI_SPEED_UNKNOWN;
  419. b->cur_bus_speed = PCI_SPEED_UNKNOWN;
  420. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  421. if (parent)
  422. b->domain_nr = parent->domain_nr;
  423. #endif
  424. return b;
  425. }
  426. static void pci_release_host_bridge_dev(struct device *dev)
  427. {
  428. struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
  429. if (bridge->release_fn)
  430. bridge->release_fn(bridge);
  431. pci_free_resource_list(&bridge->windows);
  432. kfree(bridge);
  433. }
  434. static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b)
  435. {
  436. struct pci_host_bridge *bridge;
  437. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  438. if (!bridge)
  439. return NULL;
  440. INIT_LIST_HEAD(&bridge->windows);
  441. bridge->bus = b;
  442. return bridge;
  443. }
  444. static const unsigned char pcix_bus_speed[] = {
  445. PCI_SPEED_UNKNOWN, /* 0 */
  446. PCI_SPEED_66MHz_PCIX, /* 1 */
  447. PCI_SPEED_100MHz_PCIX, /* 2 */
  448. PCI_SPEED_133MHz_PCIX, /* 3 */
  449. PCI_SPEED_UNKNOWN, /* 4 */
  450. PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
  451. PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
  452. PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
  453. PCI_SPEED_UNKNOWN, /* 8 */
  454. PCI_SPEED_66MHz_PCIX_266, /* 9 */
  455. PCI_SPEED_100MHz_PCIX_266, /* A */
  456. PCI_SPEED_133MHz_PCIX_266, /* B */
  457. PCI_SPEED_UNKNOWN, /* C */
  458. PCI_SPEED_66MHz_PCIX_533, /* D */
  459. PCI_SPEED_100MHz_PCIX_533, /* E */
  460. PCI_SPEED_133MHz_PCIX_533 /* F */
  461. };
  462. const unsigned char pcie_link_speed[] = {
  463. PCI_SPEED_UNKNOWN, /* 0 */
  464. PCIE_SPEED_2_5GT, /* 1 */
  465. PCIE_SPEED_5_0GT, /* 2 */
  466. PCIE_SPEED_8_0GT, /* 3 */
  467. PCI_SPEED_UNKNOWN, /* 4 */
  468. PCI_SPEED_UNKNOWN, /* 5 */
  469. PCI_SPEED_UNKNOWN, /* 6 */
  470. PCI_SPEED_UNKNOWN, /* 7 */
  471. PCI_SPEED_UNKNOWN, /* 8 */
  472. PCI_SPEED_UNKNOWN, /* 9 */
  473. PCI_SPEED_UNKNOWN, /* A */
  474. PCI_SPEED_UNKNOWN, /* B */
  475. PCI_SPEED_UNKNOWN, /* C */
  476. PCI_SPEED_UNKNOWN, /* D */
  477. PCI_SPEED_UNKNOWN, /* E */
  478. PCI_SPEED_UNKNOWN /* F */
  479. };
  480. void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
  481. {
  482. bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
  483. }
  484. EXPORT_SYMBOL_GPL(pcie_update_link_speed);
  485. static unsigned char agp_speeds[] = {
  486. AGP_UNKNOWN,
  487. AGP_1X,
  488. AGP_2X,
  489. AGP_4X,
  490. AGP_8X
  491. };
  492. static enum pci_bus_speed agp_speed(int agp3, int agpstat)
  493. {
  494. int index = 0;
  495. if (agpstat & 4)
  496. index = 3;
  497. else if (agpstat & 2)
  498. index = 2;
  499. else if (agpstat & 1)
  500. index = 1;
  501. else
  502. goto out;
  503. if (agp3) {
  504. index += 2;
  505. if (index == 5)
  506. index = 0;
  507. }
  508. out:
  509. return agp_speeds[index];
  510. }
  511. static void pci_set_bus_speed(struct pci_bus *bus)
  512. {
  513. struct pci_dev *bridge = bus->self;
  514. int pos;
  515. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
  516. if (!pos)
  517. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
  518. if (pos) {
  519. u32 agpstat, agpcmd;
  520. pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
  521. bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
  522. pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
  523. bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
  524. }
  525. pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  526. if (pos) {
  527. u16 status;
  528. enum pci_bus_speed max;
  529. pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
  530. &status);
  531. if (status & PCI_X_SSTATUS_533MHZ) {
  532. max = PCI_SPEED_133MHz_PCIX_533;
  533. } else if (status & PCI_X_SSTATUS_266MHZ) {
  534. max = PCI_SPEED_133MHz_PCIX_266;
  535. } else if (status & PCI_X_SSTATUS_133MHZ) {
  536. if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
  537. max = PCI_SPEED_133MHz_PCIX_ECC;
  538. else
  539. max = PCI_SPEED_133MHz_PCIX;
  540. } else {
  541. max = PCI_SPEED_66MHz_PCIX;
  542. }
  543. bus->max_bus_speed = max;
  544. bus->cur_bus_speed = pcix_bus_speed[
  545. (status & PCI_X_SSTATUS_FREQ) >> 6];
  546. return;
  547. }
  548. if (pci_is_pcie(bridge)) {
  549. u32 linkcap;
  550. u16 linksta;
  551. pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
  552. bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
  553. pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
  554. pcie_update_link_speed(bus, linksta);
  555. }
  556. }
  557. static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
  558. {
  559. struct irq_domain *d;
  560. /*
  561. * Any firmware interface that can resolve the msi_domain
  562. * should be called from here.
  563. */
  564. d = pci_host_bridge_of_msi_domain(bus);
  565. return d;
  566. }
  567. static void pci_set_bus_msi_domain(struct pci_bus *bus)
  568. {
  569. struct irq_domain *d;
  570. struct pci_bus *b;
  571. /*
  572. * The bus can be a root bus, a subordinate bus, or a virtual bus
  573. * created by an SR-IOV device. Walk up to the first bridge device
  574. * found or derive the domain from the host bridge.
  575. */
  576. for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
  577. if (b->self)
  578. d = dev_get_msi_domain(&b->self->dev);
  579. }
  580. if (!d)
  581. d = pci_host_bridge_msi_domain(b);
  582. dev_set_msi_domain(&bus->dev, d);
  583. }
  584. static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  585. struct pci_dev *bridge, int busnr)
  586. {
  587. struct pci_bus *child;
  588. int i;
  589. int ret;
  590. /*
  591. * Allocate a new bus, and inherit stuff from the parent..
  592. */
  593. child = pci_alloc_bus(parent);
  594. if (!child)
  595. return NULL;
  596. child->parent = parent;
  597. child->ops = parent->ops;
  598. child->msi = parent->msi;
  599. child->sysdata = parent->sysdata;
  600. child->bus_flags = parent->bus_flags;
  601. /* initialize some portions of the bus device, but don't register it
  602. * now as the parent is not properly set up yet.
  603. */
  604. child->dev.class = &pcibus_class;
  605. dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
  606. /*
  607. * Set up the primary, secondary and subordinate
  608. * bus numbers.
  609. */
  610. child->number = child->busn_res.start = busnr;
  611. child->primary = parent->busn_res.start;
  612. child->busn_res.end = 0xff;
  613. if (!bridge) {
  614. child->dev.parent = parent->bridge;
  615. goto add_dev;
  616. }
  617. child->self = bridge;
  618. child->bridge = get_device(&bridge->dev);
  619. child->dev.parent = child->bridge;
  620. pci_set_bus_of_node(child);
  621. pci_set_bus_speed(child);
  622. /* Set up default resource pointers and names.. */
  623. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  624. child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  625. child->resource[i]->name = child->name;
  626. }
  627. bridge->subordinate = child;
  628. add_dev:
  629. pci_set_bus_msi_domain(child);
  630. ret = device_register(&child->dev);
  631. WARN_ON(ret < 0);
  632. pcibios_add_bus(child);
  633. /* Create legacy_io and legacy_mem files for this bus */
  634. pci_create_legacy_files(child);
  635. return child;
  636. }
  637. struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
  638. int busnr)
  639. {
  640. struct pci_bus *child;
  641. child = pci_alloc_child_bus(parent, dev, busnr);
  642. if (child) {
  643. down_write(&pci_bus_sem);
  644. list_add_tail(&child->node, &parent->children);
  645. up_write(&pci_bus_sem);
  646. }
  647. return child;
  648. }
  649. EXPORT_SYMBOL(pci_add_new_bus);
  650. static void pci_enable_crs(struct pci_dev *pdev)
  651. {
  652. u16 root_cap = 0;
  653. /* Enable CRS Software Visibility if supported */
  654. pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
  655. if (root_cap & PCI_EXP_RTCAP_CRSVIS)
  656. pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
  657. PCI_EXP_RTCTL_CRSSVE);
  658. }
  659. /*
  660. * If it's a bridge, configure it and scan the bus behind it.
  661. * For CardBus bridges, we don't scan behind as the devices will
  662. * be handled by the bridge driver itself.
  663. *
  664. * We need to process bridges in two passes -- first we scan those
  665. * already configured by the BIOS and after we are done with all of
  666. * them, we proceed to assigning numbers to the remaining buses in
  667. * order to avoid overlaps between old and new bus numbers.
  668. */
  669. int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
  670. {
  671. struct pci_bus *child;
  672. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  673. u32 buses, i, j = 0;
  674. u16 bctl;
  675. u8 primary, secondary, subordinate;
  676. int broken = 0;
  677. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  678. primary = buses & 0xFF;
  679. secondary = (buses >> 8) & 0xFF;
  680. subordinate = (buses >> 16) & 0xFF;
  681. dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
  682. secondary, subordinate, pass);
  683. if (!primary && (primary != bus->number) && secondary && subordinate) {
  684. dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
  685. primary = bus->number;
  686. }
  687. /* Check if setup is sensible at all */
  688. if (!pass &&
  689. (primary != bus->number || secondary <= bus->number ||
  690. secondary > subordinate)) {
  691. dev_info(&dev->dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
  692. secondary, subordinate);
  693. broken = 1;
  694. }
  695. /* Disable MasterAbortMode during probing to avoid reporting
  696. of bus errors (in some architectures) */
  697. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  698. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  699. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  700. pci_enable_crs(dev);
  701. if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
  702. !is_cardbus && !broken) {
  703. unsigned int cmax;
  704. /*
  705. * Bus already configured by firmware, process it in the first
  706. * pass and just note the configuration.
  707. */
  708. if (pass)
  709. goto out;
  710. /*
  711. * The bus might already exist for two reasons: Either we are
  712. * rescanning the bus or the bus is reachable through more than
  713. * one bridge. The second case can happen with the i450NX
  714. * chipset.
  715. */
  716. child = pci_find_bus(pci_domain_nr(bus), secondary);
  717. if (!child) {
  718. child = pci_add_new_bus(bus, dev, secondary);
  719. if (!child)
  720. goto out;
  721. child->primary = primary;
  722. pci_bus_insert_busn_res(child, secondary, subordinate);
  723. child->bridge_ctl = bctl;
  724. }
  725. cmax = pci_scan_child_bus(child);
  726. if (cmax > subordinate)
  727. dev_warn(&dev->dev, "bridge has subordinate %02x but max busn %02x\n",
  728. subordinate, cmax);
  729. /* subordinate should equal child->busn_res.end */
  730. if (subordinate > max)
  731. max = subordinate;
  732. } else {
  733. /*
  734. * We need to assign a number to this bus which we always
  735. * do in the second pass.
  736. */
  737. if (!pass) {
  738. if (pcibios_assign_all_busses() || broken || is_cardbus)
  739. /* Temporarily disable forwarding of the
  740. configuration cycles on all bridges in
  741. this bus segment to avoid possible
  742. conflicts in the second pass between two
  743. bridges programmed with overlapping
  744. bus ranges. */
  745. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  746. buses & ~0xffffff);
  747. goto out;
  748. }
  749. /* Clear errors */
  750. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  751. /* Prevent assigning a bus number that already exists.
  752. * This can happen when a bridge is hot-plugged, so in
  753. * this case we only re-scan this bus. */
  754. child = pci_find_bus(pci_domain_nr(bus), max+1);
  755. if (!child) {
  756. child = pci_add_new_bus(bus, dev, max+1);
  757. if (!child)
  758. goto out;
  759. pci_bus_insert_busn_res(child, max+1, 0xff);
  760. }
  761. max++;
  762. buses = (buses & 0xff000000)
  763. | ((unsigned int)(child->primary) << 0)
  764. | ((unsigned int)(child->busn_res.start) << 8)
  765. | ((unsigned int)(child->busn_res.end) << 16);
  766. /*
  767. * yenta.c forces a secondary latency timer of 176.
  768. * Copy that behaviour here.
  769. */
  770. if (is_cardbus) {
  771. buses &= ~0xff000000;
  772. buses |= CARDBUS_LATENCY_TIMER << 24;
  773. }
  774. /*
  775. * We need to blast all three values with a single write.
  776. */
  777. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  778. if (!is_cardbus) {
  779. child->bridge_ctl = bctl;
  780. max = pci_scan_child_bus(child);
  781. } else {
  782. /*
  783. * For CardBus bridges, we leave 4 bus numbers
  784. * as cards with a PCI-to-PCI bridge can be
  785. * inserted later.
  786. */
  787. for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
  788. struct pci_bus *parent = bus;
  789. if (pci_find_bus(pci_domain_nr(bus),
  790. max+i+1))
  791. break;
  792. while (parent->parent) {
  793. if ((!pcibios_assign_all_busses()) &&
  794. (parent->busn_res.end > max) &&
  795. (parent->busn_res.end <= max+i)) {
  796. j = 1;
  797. }
  798. parent = parent->parent;
  799. }
  800. if (j) {
  801. /*
  802. * Often, there are two cardbus bridges
  803. * -- try to leave one valid bus number
  804. * for each one.
  805. */
  806. i /= 2;
  807. break;
  808. }
  809. }
  810. max += i;
  811. }
  812. /*
  813. * Set the subordinate bus number to its real value.
  814. */
  815. pci_bus_update_busn_res_end(child, max);
  816. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  817. }
  818. sprintf(child->name,
  819. (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
  820. pci_domain_nr(bus), child->number);
  821. /* Has only triggered on CardBus, fixup is in yenta_socket */
  822. while (bus->parent) {
  823. if ((child->busn_res.end > bus->busn_res.end) ||
  824. (child->number > bus->busn_res.end) ||
  825. (child->number < bus->number) ||
  826. (child->busn_res.end < bus->number)) {
  827. dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
  828. &child->busn_res,
  829. (bus->number > child->busn_res.end &&
  830. bus->busn_res.end < child->number) ?
  831. "wholly" : "partially",
  832. bus->self->transparent ? " transparent" : "",
  833. dev_name(&bus->dev),
  834. &bus->busn_res);
  835. }
  836. bus = bus->parent;
  837. }
  838. out:
  839. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  840. return max;
  841. }
  842. EXPORT_SYMBOL(pci_scan_bridge);
  843. /*
  844. * Read interrupt line and base address registers.
  845. * The architecture-dependent code can tweak these, of course.
  846. */
  847. static void pci_read_irq(struct pci_dev *dev)
  848. {
  849. unsigned char irq;
  850. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  851. dev->pin = irq;
  852. if (irq)
  853. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  854. dev->irq = irq;
  855. }
  856. void set_pcie_port_type(struct pci_dev *pdev)
  857. {
  858. int pos;
  859. u16 reg16;
  860. int type;
  861. struct pci_dev *parent;
  862. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  863. if (!pos)
  864. return;
  865. pdev->pcie_cap = pos;
  866. pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  867. pdev->pcie_flags_reg = reg16;
  868. pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
  869. pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
  870. /*
  871. * A Root Port is always the upstream end of a Link. No PCIe
  872. * component has two Links. Two Links are connected by a Switch
  873. * that has a Port on each Link and internal logic to connect the
  874. * two Ports.
  875. */
  876. type = pci_pcie_type(pdev);
  877. if (type == PCI_EXP_TYPE_ROOT_PORT)
  878. pdev->has_secondary_link = 1;
  879. else if (type == PCI_EXP_TYPE_UPSTREAM ||
  880. type == PCI_EXP_TYPE_DOWNSTREAM) {
  881. parent = pci_upstream_bridge(pdev);
  882. /*
  883. * Usually there's an upstream device (Root Port or Switch
  884. * Downstream Port), but we can't assume one exists.
  885. */
  886. if (parent && !parent->has_secondary_link)
  887. pdev->has_secondary_link = 1;
  888. }
  889. }
  890. void set_pcie_hotplug_bridge(struct pci_dev *pdev)
  891. {
  892. u32 reg32;
  893. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
  894. if (reg32 & PCI_EXP_SLTCAP_HPC)
  895. pdev->is_hotplug_bridge = 1;
  896. }
  897. /**
  898. * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
  899. * @dev: PCI device
  900. *
  901. * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
  902. * when forwarding a type1 configuration request the bridge must check that
  903. * the extended register address field is zero. The bridge is not permitted
  904. * to forward the transactions and must handle it as an Unsupported Request.
  905. * Some bridges do not follow this rule and simply drop the extended register
  906. * bits, resulting in the standard config space being aliased, every 256
  907. * bytes across the entire configuration space. Test for this condition by
  908. * comparing the first dword of each potential alias to the vendor/device ID.
  909. * Known offenders:
  910. * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
  911. * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
  912. */
  913. static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
  914. {
  915. #ifdef CONFIG_PCI_QUIRKS
  916. int pos;
  917. u32 header, tmp;
  918. pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
  919. for (pos = PCI_CFG_SPACE_SIZE;
  920. pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
  921. if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
  922. || header != tmp)
  923. return false;
  924. }
  925. return true;
  926. #else
  927. return false;
  928. #endif
  929. }
  930. /**
  931. * pci_cfg_space_size - get the configuration space size of the PCI device.
  932. * @dev: PCI device
  933. *
  934. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  935. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  936. * access it. Maybe we don't have a way to generate extended config space
  937. * accesses, or the device is behind a reverse Express bridge. So we try
  938. * reading the dword at 0x100 which must either be 0 or a valid extended
  939. * capability header.
  940. */
  941. static int pci_cfg_space_size_ext(struct pci_dev *dev)
  942. {
  943. u32 status;
  944. int pos = PCI_CFG_SPACE_SIZE;
  945. if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
  946. goto fail;
  947. if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
  948. goto fail;
  949. return PCI_CFG_SPACE_EXP_SIZE;
  950. fail:
  951. return PCI_CFG_SPACE_SIZE;
  952. }
  953. int pci_cfg_space_size(struct pci_dev *dev)
  954. {
  955. int pos;
  956. u32 status;
  957. u16 class;
  958. class = dev->class >> 8;
  959. if (class == PCI_CLASS_BRIDGE_HOST)
  960. return pci_cfg_space_size_ext(dev);
  961. if (!pci_is_pcie(dev)) {
  962. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  963. if (!pos)
  964. goto fail;
  965. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  966. if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  967. goto fail;
  968. }
  969. return pci_cfg_space_size_ext(dev);
  970. fail:
  971. return PCI_CFG_SPACE_SIZE;
  972. }
  973. #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  974. void pci_msi_setup_pci_dev(struct pci_dev *dev)
  975. {
  976. /*
  977. * Disable the MSI hardware to avoid screaming interrupts
  978. * during boot. This is the power on reset default so
  979. * usually this should be a noop.
  980. */
  981. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  982. if (dev->msi_cap)
  983. pci_msi_set_enable(dev, 0);
  984. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  985. if (dev->msix_cap)
  986. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  987. }
  988. /**
  989. * pci_setup_device - fill in class and map information of a device
  990. * @dev: the device structure to fill
  991. *
  992. * Initialize the device structure with information about the device's
  993. * vendor,class,memory and IO-space addresses,IRQ lines etc.
  994. * Called at initialisation of the PCI subsystem and by CardBus services.
  995. * Returns 0 on success and negative if unknown type of device (not normal,
  996. * bridge or CardBus).
  997. */
  998. int pci_setup_device(struct pci_dev *dev)
  999. {
  1000. u32 class;
  1001. u8 hdr_type;
  1002. int pos = 0;
  1003. struct pci_bus_region region;
  1004. struct resource *res;
  1005. if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
  1006. return -EIO;
  1007. dev->sysdata = dev->bus->sysdata;
  1008. dev->dev.parent = dev->bus->bridge;
  1009. dev->dev.bus = &pci_bus_type;
  1010. dev->hdr_type = hdr_type & 0x7f;
  1011. dev->multifunction = !!(hdr_type & 0x80);
  1012. dev->error_state = pci_channel_io_normal;
  1013. set_pcie_port_type(dev);
  1014. pci_dev_assign_slot(dev);
  1015. /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  1016. set this higher, assuming the system even supports it. */
  1017. dev->dma_mask = 0xffffffff;
  1018. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  1019. dev->bus->number, PCI_SLOT(dev->devfn),
  1020. PCI_FUNC(dev->devfn));
  1021. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  1022. dev->revision = class & 0xff;
  1023. dev->class = class >> 8; /* upper 3 bytes */
  1024. dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
  1025. dev->vendor, dev->device, dev->hdr_type, dev->class);
  1026. /* need to have dev->class ready */
  1027. dev->cfg_size = pci_cfg_space_size(dev);
  1028. /* "Unknown power state" */
  1029. dev->current_state = PCI_UNKNOWN;
  1030. pci_msi_setup_pci_dev(dev);
  1031. /* Early fixups, before probing the BARs */
  1032. pci_fixup_device(pci_fixup_early, dev);
  1033. /* device class may be changed after fixup */
  1034. class = dev->class >> 8;
  1035. switch (dev->hdr_type) { /* header type */
  1036. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  1037. if (class == PCI_CLASS_BRIDGE_PCI)
  1038. goto bad;
  1039. pci_read_irq(dev);
  1040. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  1041. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1042. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  1043. /*
  1044. * Do the ugly legacy mode stuff here rather than broken chip
  1045. * quirk code. Legacy mode ATA controllers have fixed
  1046. * addresses. These are not always echoed in BAR0-3, and
  1047. * BAR0-3 in a few cases contain junk!
  1048. */
  1049. if (class == PCI_CLASS_STORAGE_IDE) {
  1050. u8 progif;
  1051. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1052. if ((progif & 1) == 0) {
  1053. region.start = 0x1F0;
  1054. region.end = 0x1F7;
  1055. res = &dev->resource[0];
  1056. res->flags = LEGACY_IO_RESOURCE;
  1057. pcibios_bus_to_resource(dev->bus, res, &region);
  1058. dev_info(&dev->dev, "legacy IDE quirk: reg 0x10: %pR\n",
  1059. res);
  1060. region.start = 0x3F6;
  1061. region.end = 0x3F6;
  1062. res = &dev->resource[1];
  1063. res->flags = LEGACY_IO_RESOURCE;
  1064. pcibios_bus_to_resource(dev->bus, res, &region);
  1065. dev_info(&dev->dev, "legacy IDE quirk: reg 0x14: %pR\n",
  1066. res);
  1067. }
  1068. if ((progif & 4) == 0) {
  1069. region.start = 0x170;
  1070. region.end = 0x177;
  1071. res = &dev->resource[2];
  1072. res->flags = LEGACY_IO_RESOURCE;
  1073. pcibios_bus_to_resource(dev->bus, res, &region);
  1074. dev_info(&dev->dev, "legacy IDE quirk: reg 0x18: %pR\n",
  1075. res);
  1076. region.start = 0x376;
  1077. region.end = 0x376;
  1078. res = &dev->resource[3];
  1079. res->flags = LEGACY_IO_RESOURCE;
  1080. pcibios_bus_to_resource(dev->bus, res, &region);
  1081. dev_info(&dev->dev, "legacy IDE quirk: reg 0x1c: %pR\n",
  1082. res);
  1083. }
  1084. }
  1085. break;
  1086. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  1087. if (class != PCI_CLASS_BRIDGE_PCI)
  1088. goto bad;
  1089. /* The PCI-to-PCI bridge spec requires that subtractive
  1090. decoding (i.e. transparent) bridge must have programming
  1091. interface code of 0x01. */
  1092. pci_read_irq(dev);
  1093. dev->transparent = ((dev->class & 0xff) == 1);
  1094. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  1095. set_pcie_hotplug_bridge(dev);
  1096. pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
  1097. if (pos) {
  1098. pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
  1099. pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
  1100. }
  1101. break;
  1102. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  1103. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  1104. goto bad;
  1105. pci_read_irq(dev);
  1106. pci_read_bases(dev, 1, 0);
  1107. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1108. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  1109. break;
  1110. default: /* unknown header */
  1111. dev_err(&dev->dev, "unknown header type %02x, ignoring device\n",
  1112. dev->hdr_type);
  1113. return -EIO;
  1114. bad:
  1115. dev_err(&dev->dev, "ignoring class %#08x (doesn't match header type %02x)\n",
  1116. dev->class, dev->hdr_type);
  1117. dev->class = PCI_CLASS_NOT_DEFINED << 8;
  1118. }
  1119. /* We found a fine healthy device, go go go... */
  1120. return 0;
  1121. }
  1122. static void pci_configure_mps(struct pci_dev *dev)
  1123. {
  1124. struct pci_dev *bridge = pci_upstream_bridge(dev);
  1125. int mps, p_mps, rc;
  1126. if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
  1127. return;
  1128. mps = pcie_get_mps(dev);
  1129. p_mps = pcie_get_mps(bridge);
  1130. if (mps == p_mps)
  1131. return;
  1132. if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
  1133. dev_warn(&dev->dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1134. mps, pci_name(bridge), p_mps);
  1135. return;
  1136. }
  1137. /*
  1138. * Fancier MPS configuration is done later by
  1139. * pcie_bus_configure_settings()
  1140. */
  1141. if (pcie_bus_config != PCIE_BUS_DEFAULT)
  1142. return;
  1143. rc = pcie_set_mps(dev, p_mps);
  1144. if (rc) {
  1145. dev_warn(&dev->dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1146. p_mps);
  1147. return;
  1148. }
  1149. dev_info(&dev->dev, "Max Payload Size set to %d (was %d, max %d)\n",
  1150. p_mps, mps, 128 << dev->pcie_mpss);
  1151. }
  1152. static struct hpp_type0 pci_default_type0 = {
  1153. .revision = 1,
  1154. .cache_line_size = 8,
  1155. .latency_timer = 0x40,
  1156. .enable_serr = 0,
  1157. .enable_perr = 0,
  1158. };
  1159. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  1160. {
  1161. u16 pci_cmd, pci_bctl;
  1162. if (!hpp)
  1163. hpp = &pci_default_type0;
  1164. if (hpp->revision > 1) {
  1165. dev_warn(&dev->dev,
  1166. "PCI settings rev %d not supported; using defaults\n",
  1167. hpp->revision);
  1168. hpp = &pci_default_type0;
  1169. }
  1170. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  1171. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  1172. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1173. if (hpp->enable_serr)
  1174. pci_cmd |= PCI_COMMAND_SERR;
  1175. if (hpp->enable_perr)
  1176. pci_cmd |= PCI_COMMAND_PARITY;
  1177. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1178. /* Program bridge control value */
  1179. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1180. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1181. hpp->latency_timer);
  1182. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1183. if (hpp->enable_serr)
  1184. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1185. if (hpp->enable_perr)
  1186. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1187. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1188. }
  1189. }
  1190. static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
  1191. {
  1192. if (hpp)
  1193. dev_warn(&dev->dev, "PCI-X settings not supported\n");
  1194. }
  1195. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  1196. {
  1197. int pos;
  1198. u32 reg32;
  1199. if (!hpp)
  1200. return;
  1201. if (hpp->revision > 1) {
  1202. dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
  1203. hpp->revision);
  1204. return;
  1205. }
  1206. /*
  1207. * Don't allow _HPX to change MPS or MRRS settings. We manage
  1208. * those to make sure they're consistent with the rest of the
  1209. * platform.
  1210. */
  1211. hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
  1212. PCI_EXP_DEVCTL_READRQ;
  1213. hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
  1214. PCI_EXP_DEVCTL_READRQ);
  1215. /* Initialize Device Control Register */
  1216. pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  1217. ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
  1218. /* Initialize Link Control Register */
  1219. if (pcie_cap_has_lnkctl(dev))
  1220. pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
  1221. ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
  1222. /* Find Advanced Error Reporting Enhanced Capability */
  1223. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  1224. if (!pos)
  1225. return;
  1226. /* Initialize Uncorrectable Error Mask Register */
  1227. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  1228. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  1229. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  1230. /* Initialize Uncorrectable Error Severity Register */
  1231. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  1232. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  1233. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  1234. /* Initialize Correctable Error Mask Register */
  1235. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  1236. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  1237. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  1238. /* Initialize Advanced Error Capabilities and Control Register */
  1239. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  1240. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  1241. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  1242. /*
  1243. * FIXME: The following two registers are not supported yet.
  1244. *
  1245. * o Secondary Uncorrectable Error Severity Register
  1246. * o Secondary Uncorrectable Error Mask Register
  1247. */
  1248. }
  1249. static void pci_configure_device(struct pci_dev *dev)
  1250. {
  1251. struct hotplug_params hpp;
  1252. int ret;
  1253. pci_configure_mps(dev);
  1254. memset(&hpp, 0, sizeof(hpp));
  1255. ret = pci_get_hp_params(dev, &hpp);
  1256. if (ret)
  1257. return;
  1258. program_hpp_type2(dev, hpp.t2);
  1259. program_hpp_type1(dev, hpp.t1);
  1260. program_hpp_type0(dev, hpp.t0);
  1261. }
  1262. static void pci_release_capabilities(struct pci_dev *dev)
  1263. {
  1264. pci_vpd_release(dev);
  1265. pci_iov_release(dev);
  1266. pci_free_cap_save_buffers(dev);
  1267. }
  1268. /**
  1269. * pci_release_dev - free a pci device structure when all users of it are finished.
  1270. * @dev: device that's been disconnected
  1271. *
  1272. * Will be called only by the device core when all users of this pci device are
  1273. * done.
  1274. */
  1275. static void pci_release_dev(struct device *dev)
  1276. {
  1277. struct pci_dev *pci_dev;
  1278. pci_dev = to_pci_dev(dev);
  1279. pci_release_capabilities(pci_dev);
  1280. pci_release_of_node(pci_dev);
  1281. pcibios_release_device(pci_dev);
  1282. pci_bus_put(pci_dev->bus);
  1283. kfree(pci_dev->driver_override);
  1284. kfree(pci_dev);
  1285. }
  1286. struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
  1287. {
  1288. struct pci_dev *dev;
  1289. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1290. if (!dev)
  1291. return NULL;
  1292. INIT_LIST_HEAD(&dev->bus_list);
  1293. dev->dev.type = &pci_dev_type;
  1294. dev->bus = pci_bus_get(bus);
  1295. return dev;
  1296. }
  1297. EXPORT_SYMBOL(pci_alloc_dev);
  1298. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
  1299. int crs_timeout)
  1300. {
  1301. int delay = 1;
  1302. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1303. return false;
  1304. /* some broken boards return 0 or ~0 if a slot is empty: */
  1305. if (*l == 0xffffffff || *l == 0x00000000 ||
  1306. *l == 0x0000ffff || *l == 0xffff0000)
  1307. return false;
  1308. /*
  1309. * Configuration Request Retry Status. Some root ports return the
  1310. * actual device ID instead of the synthetic ID (0xFFFF) required
  1311. * by the PCIe spec. Ignore the device ID and only check for
  1312. * (vendor id == 1).
  1313. */
  1314. while ((*l & 0xffff) == 0x0001) {
  1315. if (!crs_timeout)
  1316. return false;
  1317. msleep(delay);
  1318. delay *= 2;
  1319. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1320. return false;
  1321. /* Card hasn't responded in 60 seconds? Must be stuck. */
  1322. if (delay > crs_timeout) {
  1323. printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not responding\n",
  1324. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  1325. PCI_FUNC(devfn));
  1326. return false;
  1327. }
  1328. }
  1329. return true;
  1330. }
  1331. EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
  1332. /*
  1333. * Read the config data for a PCI device, sanity-check it
  1334. * and fill in the dev structure...
  1335. */
  1336. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  1337. {
  1338. struct pci_dev *dev;
  1339. u32 l;
  1340. if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
  1341. return NULL;
  1342. dev = pci_alloc_dev(bus);
  1343. if (!dev)
  1344. return NULL;
  1345. dev->devfn = devfn;
  1346. dev->vendor = l & 0xffff;
  1347. dev->device = (l >> 16) & 0xffff;
  1348. pci_set_of_node(dev);
  1349. if (pci_setup_device(dev)) {
  1350. pci_bus_put(dev->bus);
  1351. kfree(dev);
  1352. return NULL;
  1353. }
  1354. return dev;
  1355. }
  1356. static void pci_init_capabilities(struct pci_dev *dev)
  1357. {
  1358. /* MSI/MSI-X list */
  1359. pci_msi_init_pci_dev(dev);
  1360. /* Buffers for saving PCIe and PCI-X capabilities */
  1361. pci_allocate_cap_save_buffers(dev);
  1362. /* Power Management */
  1363. pci_pm_init(dev);
  1364. /* Vital Product Data */
  1365. pci_vpd_pci22_init(dev);
  1366. /* Alternative Routing-ID Forwarding */
  1367. pci_configure_ari(dev);
  1368. /* Single Root I/O Virtualization */
  1369. pci_iov_init(dev);
  1370. /* Address Translation Services */
  1371. pci_ats_init(dev);
  1372. /* Enable ACS P2P upstream forwarding */
  1373. pci_enable_acs(dev);
  1374. }
  1375. static void pci_set_msi_domain(struct pci_dev *dev)
  1376. {
  1377. /*
  1378. * If no domain has been set through the pcibios_add_device
  1379. * callback, inherit the default from the bus device.
  1380. */
  1381. if (!dev_get_msi_domain(&dev->dev))
  1382. dev_set_msi_domain(&dev->dev,
  1383. dev_get_msi_domain(&dev->bus->dev));
  1384. }
  1385. void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  1386. {
  1387. int ret;
  1388. pci_configure_device(dev);
  1389. device_initialize(&dev->dev);
  1390. dev->dev.release = pci_release_dev;
  1391. set_dev_node(&dev->dev, pcibus_to_node(bus));
  1392. dev->dev.dma_mask = &dev->dma_mask;
  1393. dev->dev.dma_parms = &dev->dma_parms;
  1394. dev->dev.coherent_dma_mask = 0xffffffffull;
  1395. of_pci_dma_configure(dev);
  1396. pci_set_dma_max_seg_size(dev, 65536);
  1397. pci_set_dma_seg_boundary(dev, 0xffffffff);
  1398. /* Fix up broken headers */
  1399. pci_fixup_device(pci_fixup_header, dev);
  1400. /* moved out from quirk header fixup code */
  1401. pci_reassigndev_resource_alignment(dev);
  1402. /* Clear the state_saved flag. */
  1403. dev->state_saved = false;
  1404. /* Initialize various capabilities */
  1405. pci_init_capabilities(dev);
  1406. /*
  1407. * Add the device to our list of discovered devices
  1408. * and the bus list for fixup functions, etc.
  1409. */
  1410. down_write(&pci_bus_sem);
  1411. list_add_tail(&dev->bus_list, &bus->devices);
  1412. up_write(&pci_bus_sem);
  1413. ret = pcibios_add_device(dev);
  1414. WARN_ON(ret < 0);
  1415. /* Setup MSI irq domain */
  1416. pci_set_msi_domain(dev);
  1417. /* Notifier could use PCI capabilities */
  1418. dev->match_driver = false;
  1419. ret = device_add(&dev->dev);
  1420. WARN_ON(ret < 0);
  1421. }
  1422. struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
  1423. {
  1424. struct pci_dev *dev;
  1425. dev = pci_get_slot(bus, devfn);
  1426. if (dev) {
  1427. pci_dev_put(dev);
  1428. return dev;
  1429. }
  1430. dev = pci_scan_device(bus, devfn);
  1431. if (!dev)
  1432. return NULL;
  1433. pci_device_add(dev, bus);
  1434. return dev;
  1435. }
  1436. EXPORT_SYMBOL(pci_scan_single_device);
  1437. static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
  1438. {
  1439. int pos;
  1440. u16 cap = 0;
  1441. unsigned next_fn;
  1442. if (pci_ari_enabled(bus)) {
  1443. if (!dev)
  1444. return 0;
  1445. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1446. if (!pos)
  1447. return 0;
  1448. pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
  1449. next_fn = PCI_ARI_CAP_NFN(cap);
  1450. if (next_fn <= fn)
  1451. return 0; /* protect against malformed list */
  1452. return next_fn;
  1453. }
  1454. /* dev may be NULL for non-contiguous multifunction devices */
  1455. if (!dev || dev->multifunction)
  1456. return (fn + 1) % 8;
  1457. return 0;
  1458. }
  1459. static int only_one_child(struct pci_bus *bus)
  1460. {
  1461. struct pci_dev *parent = bus->self;
  1462. if (!parent || !pci_is_pcie(parent))
  1463. return 0;
  1464. if (pci_pcie_type(parent) == PCI_EXP_TYPE_ROOT_PORT)
  1465. return 1;
  1466. if (parent->has_secondary_link &&
  1467. !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
  1468. return 1;
  1469. return 0;
  1470. }
  1471. /**
  1472. * pci_scan_slot - scan a PCI slot on a bus for devices.
  1473. * @bus: PCI bus to scan
  1474. * @devfn: slot number to scan (must have zero function.)
  1475. *
  1476. * Scan a PCI slot on the specified PCI bus for devices, adding
  1477. * discovered devices to the @bus->devices list. New devices
  1478. * will not have is_added set.
  1479. *
  1480. * Returns the number of new devices found.
  1481. */
  1482. int pci_scan_slot(struct pci_bus *bus, int devfn)
  1483. {
  1484. unsigned fn, nr = 0;
  1485. struct pci_dev *dev;
  1486. if (only_one_child(bus) && (devfn > 0))
  1487. return 0; /* Already scanned the entire slot */
  1488. dev = pci_scan_single_device(bus, devfn);
  1489. if (!dev)
  1490. return 0;
  1491. if (!dev->is_added)
  1492. nr++;
  1493. for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
  1494. dev = pci_scan_single_device(bus, devfn + fn);
  1495. if (dev) {
  1496. if (!dev->is_added)
  1497. nr++;
  1498. dev->multifunction = 1;
  1499. }
  1500. }
  1501. /* only one slot has pcie device */
  1502. if (bus->self && nr)
  1503. pcie_aspm_init_link_state(bus->self);
  1504. return nr;
  1505. }
  1506. EXPORT_SYMBOL(pci_scan_slot);
  1507. static int pcie_find_smpss(struct pci_dev *dev, void *data)
  1508. {
  1509. u8 *smpss = data;
  1510. if (!pci_is_pcie(dev))
  1511. return 0;
  1512. /*
  1513. * We don't have a way to change MPS settings on devices that have
  1514. * drivers attached. A hot-added device might support only the minimum
  1515. * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
  1516. * where devices may be hot-added, we limit the fabric MPS to 128 so
  1517. * hot-added devices will work correctly.
  1518. *
  1519. * However, if we hot-add a device to a slot directly below a Root
  1520. * Port, it's impossible for there to be other existing devices below
  1521. * the port. We don't limit the MPS in this case because we can
  1522. * reconfigure MPS on both the Root Port and the hot-added device,
  1523. * and there are no other devices involved.
  1524. *
  1525. * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
  1526. */
  1527. if (dev->is_hotplug_bridge &&
  1528. pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
  1529. *smpss = 0;
  1530. if (*smpss > dev->pcie_mpss)
  1531. *smpss = dev->pcie_mpss;
  1532. return 0;
  1533. }
  1534. static void pcie_write_mps(struct pci_dev *dev, int mps)
  1535. {
  1536. int rc;
  1537. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  1538. mps = 128 << dev->pcie_mpss;
  1539. if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
  1540. dev->bus->self)
  1541. /* For "Performance", the assumption is made that
  1542. * downstream communication will never be larger than
  1543. * the MRRS. So, the MPS only needs to be configured
  1544. * for the upstream communication. This being the case,
  1545. * walk from the top down and set the MPS of the child
  1546. * to that of the parent bus.
  1547. *
  1548. * Configure the device MPS with the smaller of the
  1549. * device MPSS or the bridge MPS (which is assumed to be
  1550. * properly configured at this point to the largest
  1551. * allowable MPS based on its parent bus).
  1552. */
  1553. mps = min(mps, pcie_get_mps(dev->bus->self));
  1554. }
  1555. rc = pcie_set_mps(dev, mps);
  1556. if (rc)
  1557. dev_err(&dev->dev, "Failed attempting to set the MPS\n");
  1558. }
  1559. static void pcie_write_mrrs(struct pci_dev *dev)
  1560. {
  1561. int rc, mrrs;
  1562. /* In the "safe" case, do not configure the MRRS. There appear to be
  1563. * issues with setting MRRS to 0 on a number of devices.
  1564. */
  1565. if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
  1566. return;
  1567. /* For Max performance, the MRRS must be set to the largest supported
  1568. * value. However, it cannot be configured larger than the MPS the
  1569. * device or the bus can support. This should already be properly
  1570. * configured by a prior call to pcie_write_mps.
  1571. */
  1572. mrrs = pcie_get_mps(dev);
  1573. /* MRRS is a R/W register. Invalid values can be written, but a
  1574. * subsequent read will verify if the value is acceptable or not.
  1575. * If the MRRS value provided is not acceptable (e.g., too large),
  1576. * shrink the value until it is acceptable to the HW.
  1577. */
  1578. while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
  1579. rc = pcie_set_readrq(dev, mrrs);
  1580. if (!rc)
  1581. break;
  1582. dev_warn(&dev->dev, "Failed attempting to set the MRRS\n");
  1583. mrrs /= 2;
  1584. }
  1585. if (mrrs < 128)
  1586. dev_err(&dev->dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n");
  1587. }
  1588. static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
  1589. {
  1590. int mps, orig_mps;
  1591. if (!pci_is_pcie(dev))
  1592. return 0;
  1593. if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
  1594. pcie_bus_config == PCIE_BUS_DEFAULT)
  1595. return 0;
  1596. mps = 128 << *(u8 *)data;
  1597. orig_mps = pcie_get_mps(dev);
  1598. pcie_write_mps(dev, mps);
  1599. pcie_write_mrrs(dev);
  1600. dev_info(&dev->dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
  1601. pcie_get_mps(dev), 128 << dev->pcie_mpss,
  1602. orig_mps, pcie_get_readrq(dev));
  1603. return 0;
  1604. }
  1605. /* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down,
  1606. * parents then children fashion. If this changes, then this code will not
  1607. * work as designed.
  1608. */
  1609. void pcie_bus_configure_settings(struct pci_bus *bus)
  1610. {
  1611. u8 smpss = 0;
  1612. if (!bus->self)
  1613. return;
  1614. if (!pci_is_pcie(bus->self))
  1615. return;
  1616. /* FIXME - Peer to peer DMA is possible, though the endpoint would need
  1617. * to be aware of the MPS of the destination. To work around this,
  1618. * simply force the MPS of the entire system to the smallest possible.
  1619. */
  1620. if (pcie_bus_config == PCIE_BUS_PEER2PEER)
  1621. smpss = 0;
  1622. if (pcie_bus_config == PCIE_BUS_SAFE) {
  1623. smpss = bus->self->pcie_mpss;
  1624. pcie_find_smpss(bus->self, &smpss);
  1625. pci_walk_bus(bus, pcie_find_smpss, &smpss);
  1626. }
  1627. pcie_bus_configure_set(bus->self, &smpss);
  1628. pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
  1629. }
  1630. EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
  1631. unsigned int pci_scan_child_bus(struct pci_bus *bus)
  1632. {
  1633. unsigned int devfn, pass, max = bus->busn_res.start;
  1634. struct pci_dev *dev;
  1635. dev_dbg(&bus->dev, "scanning bus\n");
  1636. /* Go find them, Rover! */
  1637. for (devfn = 0; devfn < 0x100; devfn += 8)
  1638. pci_scan_slot(bus, devfn);
  1639. /* Reserve buses for SR-IOV capability. */
  1640. max += pci_iov_bus_range(bus);
  1641. /*
  1642. * After performing arch-dependent fixup of the bus, look behind
  1643. * all PCI-to-PCI bridges on this bus.
  1644. */
  1645. if (!bus->is_added) {
  1646. dev_dbg(&bus->dev, "fixups for bus\n");
  1647. pcibios_fixup_bus(bus);
  1648. bus->is_added = 1;
  1649. }
  1650. for (pass = 0; pass < 2; pass++)
  1651. list_for_each_entry(dev, &bus->devices, bus_list) {
  1652. if (pci_is_bridge(dev))
  1653. max = pci_scan_bridge(bus, dev, max, pass);
  1654. }
  1655. /*
  1656. * We've scanned the bus and so we know all about what's on
  1657. * the other side of any bridges that may be on this bus plus
  1658. * any devices.
  1659. *
  1660. * Return how far we've got finding sub-buses.
  1661. */
  1662. dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
  1663. return max;
  1664. }
  1665. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  1666. /**
  1667. * pcibios_root_bridge_prepare - Platform-specific host bridge setup.
  1668. * @bridge: Host bridge to set up.
  1669. *
  1670. * Default empty implementation. Replace with an architecture-specific setup
  1671. * routine, if necessary.
  1672. */
  1673. int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  1674. {
  1675. return 0;
  1676. }
  1677. void __weak pcibios_add_bus(struct pci_bus *bus)
  1678. {
  1679. }
  1680. void __weak pcibios_remove_bus(struct pci_bus *bus)
  1681. {
  1682. }
  1683. struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
  1684. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  1685. {
  1686. int error;
  1687. struct pci_host_bridge *bridge;
  1688. struct pci_bus *b, *b2;
  1689. struct resource_entry *window, *n;
  1690. struct resource *res;
  1691. resource_size_t offset;
  1692. char bus_addr[64];
  1693. char *fmt;
  1694. b = pci_alloc_bus(NULL);
  1695. if (!b)
  1696. return NULL;
  1697. b->sysdata = sysdata;
  1698. b->ops = ops;
  1699. b->number = b->busn_res.start = bus;
  1700. pci_bus_assign_domain_nr(b, parent);
  1701. b2 = pci_find_bus(pci_domain_nr(b), bus);
  1702. if (b2) {
  1703. /* If we already got to this bus through a different bridge, ignore it */
  1704. dev_dbg(&b2->dev, "bus already known\n");
  1705. goto err_out;
  1706. }
  1707. bridge = pci_alloc_host_bridge(b);
  1708. if (!bridge)
  1709. goto err_out;
  1710. bridge->dev.parent = parent;
  1711. bridge->dev.release = pci_release_host_bridge_dev;
  1712. dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus);
  1713. error = pcibios_root_bridge_prepare(bridge);
  1714. if (error) {
  1715. kfree(bridge);
  1716. goto err_out;
  1717. }
  1718. error = device_register(&bridge->dev);
  1719. if (error) {
  1720. put_device(&bridge->dev);
  1721. goto err_out;
  1722. }
  1723. b->bridge = get_device(&bridge->dev);
  1724. device_enable_async_suspend(b->bridge);
  1725. pci_set_bus_of_node(b);
  1726. pci_set_bus_msi_domain(b);
  1727. if (!parent)
  1728. set_dev_node(b->bridge, pcibus_to_node(b));
  1729. b->dev.class = &pcibus_class;
  1730. b->dev.parent = b->bridge;
  1731. dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus);
  1732. error = device_register(&b->dev);
  1733. if (error)
  1734. goto class_dev_reg_err;
  1735. pcibios_add_bus(b);
  1736. /* Create legacy_io and legacy_mem files for this bus */
  1737. pci_create_legacy_files(b);
  1738. if (parent)
  1739. dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev));
  1740. else
  1741. printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev));
  1742. /* Add initial resources to the bus */
  1743. resource_list_for_each_entry_safe(window, n, resources) {
  1744. list_move_tail(&window->node, &bridge->windows);
  1745. res = window->res;
  1746. offset = window->offset;
  1747. if (res->flags & IORESOURCE_BUS)
  1748. pci_bus_insert_busn_res(b, bus, res->end);
  1749. else
  1750. pci_bus_add_resource(b, res, 0);
  1751. if (offset) {
  1752. if (resource_type(res) == IORESOURCE_IO)
  1753. fmt = " (bus address [%#06llx-%#06llx])";
  1754. else
  1755. fmt = " (bus address [%#010llx-%#010llx])";
  1756. snprintf(bus_addr, sizeof(bus_addr), fmt,
  1757. (unsigned long long) (res->start - offset),
  1758. (unsigned long long) (res->end - offset));
  1759. } else
  1760. bus_addr[0] = '\0';
  1761. dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr);
  1762. }
  1763. down_write(&pci_bus_sem);
  1764. list_add_tail(&b->node, &pci_root_buses);
  1765. up_write(&pci_bus_sem);
  1766. return b;
  1767. class_dev_reg_err:
  1768. put_device(&bridge->dev);
  1769. device_unregister(&bridge->dev);
  1770. err_out:
  1771. kfree(b);
  1772. return NULL;
  1773. }
  1774. EXPORT_SYMBOL_GPL(pci_create_root_bus);
  1775. int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
  1776. {
  1777. struct resource *res = &b->busn_res;
  1778. struct resource *parent_res, *conflict;
  1779. res->start = bus;
  1780. res->end = bus_max;
  1781. res->flags = IORESOURCE_BUS;
  1782. if (!pci_is_root_bus(b))
  1783. parent_res = &b->parent->busn_res;
  1784. else {
  1785. parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
  1786. res->flags |= IORESOURCE_PCI_FIXED;
  1787. }
  1788. conflict = request_resource_conflict(parent_res, res);
  1789. if (conflict)
  1790. dev_printk(KERN_DEBUG, &b->dev,
  1791. "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
  1792. res, pci_is_root_bus(b) ? "domain " : "",
  1793. parent_res, conflict->name, conflict);
  1794. return conflict == NULL;
  1795. }
  1796. int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
  1797. {
  1798. struct resource *res = &b->busn_res;
  1799. struct resource old_res = *res;
  1800. resource_size_t size;
  1801. int ret;
  1802. if (res->start > bus_max)
  1803. return -EINVAL;
  1804. size = bus_max - res->start + 1;
  1805. ret = adjust_resource(res, res->start, size);
  1806. dev_printk(KERN_DEBUG, &b->dev,
  1807. "busn_res: %pR end %s updated to %02x\n",
  1808. &old_res, ret ? "can not be" : "is", bus_max);
  1809. if (!ret && !res->parent)
  1810. pci_bus_insert_busn_res(b, res->start, res->end);
  1811. return ret;
  1812. }
  1813. void pci_bus_release_busn_res(struct pci_bus *b)
  1814. {
  1815. struct resource *res = &b->busn_res;
  1816. int ret;
  1817. if (!res->flags || !res->parent)
  1818. return;
  1819. ret = release_resource(res);
  1820. dev_printk(KERN_DEBUG, &b->dev,
  1821. "busn_res: %pR %s released\n",
  1822. res, ret ? "can not be" : "is");
  1823. }
  1824. struct pci_bus *pci_scan_root_bus_msi(struct device *parent, int bus,
  1825. struct pci_ops *ops, void *sysdata,
  1826. struct list_head *resources, struct msi_controller *msi)
  1827. {
  1828. struct resource_entry *window;
  1829. bool found = false;
  1830. struct pci_bus *b;
  1831. int max;
  1832. resource_list_for_each_entry(window, resources)
  1833. if (window->res->flags & IORESOURCE_BUS) {
  1834. found = true;
  1835. break;
  1836. }
  1837. b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
  1838. if (!b)
  1839. return NULL;
  1840. b->msi = msi;
  1841. if (!found) {
  1842. dev_info(&b->dev,
  1843. "No busn resource found for root bus, will use [bus %02x-ff]\n",
  1844. bus);
  1845. pci_bus_insert_busn_res(b, bus, 255);
  1846. }
  1847. max = pci_scan_child_bus(b);
  1848. if (!found)
  1849. pci_bus_update_busn_res_end(b, max);
  1850. return b;
  1851. }
  1852. struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
  1853. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  1854. {
  1855. return pci_scan_root_bus_msi(parent, bus, ops, sysdata, resources,
  1856. NULL);
  1857. }
  1858. EXPORT_SYMBOL(pci_scan_root_bus);
  1859. struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
  1860. void *sysdata)
  1861. {
  1862. LIST_HEAD(resources);
  1863. struct pci_bus *b;
  1864. pci_add_resource(&resources, &ioport_resource);
  1865. pci_add_resource(&resources, &iomem_resource);
  1866. pci_add_resource(&resources, &busn_resource);
  1867. b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
  1868. if (b) {
  1869. pci_scan_child_bus(b);
  1870. } else {
  1871. pci_free_resource_list(&resources);
  1872. }
  1873. return b;
  1874. }
  1875. EXPORT_SYMBOL(pci_scan_bus);
  1876. /**
  1877. * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
  1878. * @bridge: PCI bridge for the bus to scan
  1879. *
  1880. * Scan a PCI bus and child buses for new devices, add them,
  1881. * and enable them, resizing bridge mmio/io resource if necessary
  1882. * and possible. The caller must ensure the child devices are already
  1883. * removed for resizing to occur.
  1884. *
  1885. * Returns the max number of subordinate bus discovered.
  1886. */
  1887. unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
  1888. {
  1889. unsigned int max;
  1890. struct pci_bus *bus = bridge->subordinate;
  1891. max = pci_scan_child_bus(bus);
  1892. pci_assign_unassigned_bridge_resources(bridge);
  1893. pci_bus_add_devices(bus);
  1894. return max;
  1895. }
  1896. /**
  1897. * pci_rescan_bus - scan a PCI bus for devices.
  1898. * @bus: PCI bus to scan
  1899. *
  1900. * Scan a PCI bus and child buses for new devices, adds them,
  1901. * and enables them.
  1902. *
  1903. * Returns the max number of subordinate bus discovered.
  1904. */
  1905. unsigned int pci_rescan_bus(struct pci_bus *bus)
  1906. {
  1907. unsigned int max;
  1908. max = pci_scan_child_bus(bus);
  1909. pci_assign_unassigned_bus_resources(bus);
  1910. pci_bus_add_devices(bus);
  1911. return max;
  1912. }
  1913. EXPORT_SYMBOL_GPL(pci_rescan_bus);
  1914. /*
  1915. * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
  1916. * routines should always be executed under this mutex.
  1917. */
  1918. static DEFINE_MUTEX(pci_rescan_remove_lock);
  1919. void pci_lock_rescan_remove(void)
  1920. {
  1921. mutex_lock(&pci_rescan_remove_lock);
  1922. }
  1923. EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
  1924. void pci_unlock_rescan_remove(void)
  1925. {
  1926. mutex_unlock(&pci_rescan_remove_lock);
  1927. }
  1928. EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
  1929. static int __init pci_sort_bf_cmp(const struct device *d_a,
  1930. const struct device *d_b)
  1931. {
  1932. const struct pci_dev *a = to_pci_dev(d_a);
  1933. const struct pci_dev *b = to_pci_dev(d_b);
  1934. if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
  1935. else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
  1936. if (a->bus->number < b->bus->number) return -1;
  1937. else if (a->bus->number > b->bus->number) return 1;
  1938. if (a->devfn < b->devfn) return -1;
  1939. else if (a->devfn > b->devfn) return 1;
  1940. return 0;
  1941. }
  1942. void __init pci_sort_breadthfirst(void)
  1943. {
  1944. bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
  1945. }