probe.c 63 KB

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