probe.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI detection and setup code
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_pci.h>
  11. #include <linux/pci_hotplug.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/aer.h>
  16. #include <linux/acpi.h>
  17. #include <linux/hypervisor.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/pm_runtime.h>
  20. #include "pci.h"
  21. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  22. #define CARDBUS_RESERVE_BUSNR 3
  23. static struct resource busn_resource = {
  24. .name = "PCI busn",
  25. .start = 0,
  26. .end = 255,
  27. .flags = IORESOURCE_BUS,
  28. };
  29. /* Ugh. Need to stop exporting this to modules. */
  30. LIST_HEAD(pci_root_buses);
  31. EXPORT_SYMBOL(pci_root_buses);
  32. static LIST_HEAD(pci_domain_busn_res_list);
  33. struct pci_domain_busn_res {
  34. struct list_head list;
  35. struct resource res;
  36. int domain_nr;
  37. };
  38. static struct resource *get_pci_domain_busn_res(int domain_nr)
  39. {
  40. struct pci_domain_busn_res *r;
  41. list_for_each_entry(r, &pci_domain_busn_res_list, list)
  42. if (r->domain_nr == domain_nr)
  43. return &r->res;
  44. r = kzalloc(sizeof(*r), GFP_KERNEL);
  45. if (!r)
  46. return NULL;
  47. r->domain_nr = domain_nr;
  48. r->res.start = 0;
  49. r->res.end = 0xff;
  50. r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
  51. list_add_tail(&r->list, &pci_domain_busn_res_list);
  52. return &r->res;
  53. }
  54. static int find_anything(struct device *dev, void *data)
  55. {
  56. return 1;
  57. }
  58. /*
  59. * Some device drivers need know if PCI is initiated.
  60. * Basically, we think PCI is not initiated when there
  61. * is no device to be found on the pci_bus_type.
  62. */
  63. int no_pci_devices(void)
  64. {
  65. struct device *dev;
  66. int no_devices;
  67. dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
  68. no_devices = (dev == NULL);
  69. put_device(dev);
  70. return no_devices;
  71. }
  72. EXPORT_SYMBOL(no_pci_devices);
  73. /*
  74. * PCI Bus Class
  75. */
  76. static void release_pcibus_dev(struct device *dev)
  77. {
  78. struct pci_bus *pci_bus = to_pci_bus(dev);
  79. put_device(pci_bus->bridge);
  80. pci_bus_remove_resources(pci_bus);
  81. pci_release_bus_of_node(pci_bus);
  82. kfree(pci_bus);
  83. }
  84. static struct class pcibus_class = {
  85. .name = "pci_bus",
  86. .dev_release = &release_pcibus_dev,
  87. .dev_groups = pcibus_groups,
  88. };
  89. static int __init pcibus_class_init(void)
  90. {
  91. return class_register(&pcibus_class);
  92. }
  93. postcore_initcall(pcibus_class_init);
  94. static u64 pci_size(u64 base, u64 maxbase, u64 mask)
  95. {
  96. u64 size = mask & maxbase; /* Find the significant bits */
  97. if (!size)
  98. return 0;
  99. /*
  100. * Get the lowest of them to find the decode size, and from that
  101. * the extent.
  102. */
  103. size = (size & ~(size-1)) - 1;
  104. /*
  105. * base == maxbase can be valid only if the BAR has already been
  106. * programmed with all 1s.
  107. */
  108. if (base == maxbase && ((base | size) & mask) != mask)
  109. return 0;
  110. return size;
  111. }
  112. static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
  113. {
  114. u32 mem_type;
  115. unsigned long flags;
  116. if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  117. flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
  118. flags |= IORESOURCE_IO;
  119. return flags;
  120. }
  121. flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
  122. flags |= IORESOURCE_MEM;
  123. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  124. flags |= IORESOURCE_PREFETCH;
  125. mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  126. switch (mem_type) {
  127. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  128. break;
  129. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  130. /* 1M mem BAR treated as 32-bit BAR */
  131. break;
  132. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  133. flags |= IORESOURCE_MEM_64;
  134. break;
  135. default:
  136. /* mem unknown type treated as 32-bit BAR */
  137. break;
  138. }
  139. return flags;
  140. }
  141. #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
  142. /**
  143. * pci_read_base - Read a PCI BAR
  144. * @dev: the PCI device
  145. * @type: type of the BAR
  146. * @res: resource buffer to be filled in
  147. * @pos: BAR position in the config space
  148. *
  149. * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
  150. */
  151. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  152. struct resource *res, unsigned int pos)
  153. {
  154. u32 l = 0, sz = 0, mask;
  155. u64 l64, sz64, mask64;
  156. u16 orig_cmd;
  157. struct pci_bus_region region, inverted_region;
  158. mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
  159. /* No printks while decoding is disabled! */
  160. if (!dev->mmio_always_on) {
  161. pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
  162. if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
  163. pci_write_config_word(dev, PCI_COMMAND,
  164. orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
  165. }
  166. }
  167. res->name = pci_name(dev);
  168. pci_read_config_dword(dev, pos, &l);
  169. pci_write_config_dword(dev, pos, l | mask);
  170. pci_read_config_dword(dev, pos, &sz);
  171. pci_write_config_dword(dev, pos, l);
  172. /*
  173. * All bits set in sz means the device isn't working properly.
  174. * If the BAR isn't implemented, all bits must be 0. If it's a
  175. * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
  176. * 1 must be clear.
  177. */
  178. if (sz == 0xffffffff)
  179. sz = 0;
  180. /*
  181. * I don't know how l can have all bits set. Copied from old code.
  182. * Maybe it fixes a bug on some ancient platform.
  183. */
  184. if (l == 0xffffffff)
  185. l = 0;
  186. if (type == pci_bar_unknown) {
  187. res->flags = decode_bar(dev, l);
  188. res->flags |= IORESOURCE_SIZEALIGN;
  189. if (res->flags & IORESOURCE_IO) {
  190. l64 = l & PCI_BASE_ADDRESS_IO_MASK;
  191. sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
  192. mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
  193. } else {
  194. l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
  195. sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
  196. mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  197. }
  198. } else {
  199. if (l & PCI_ROM_ADDRESS_ENABLE)
  200. res->flags |= IORESOURCE_ROM_ENABLE;
  201. l64 = l & PCI_ROM_ADDRESS_MASK;
  202. sz64 = sz & PCI_ROM_ADDRESS_MASK;
  203. mask64 = PCI_ROM_ADDRESS_MASK;
  204. }
  205. if (res->flags & IORESOURCE_MEM_64) {
  206. pci_read_config_dword(dev, pos + 4, &l);
  207. pci_write_config_dword(dev, pos + 4, ~0);
  208. pci_read_config_dword(dev, pos + 4, &sz);
  209. pci_write_config_dword(dev, pos + 4, l);
  210. l64 |= ((u64)l << 32);
  211. sz64 |= ((u64)sz << 32);
  212. mask64 |= ((u64)~0 << 32);
  213. }
  214. if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
  215. pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
  216. if (!sz64)
  217. goto fail;
  218. sz64 = pci_size(l64, sz64, mask64);
  219. if (!sz64) {
  220. pci_info(dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
  221. pos);
  222. goto fail;
  223. }
  224. if (res->flags & IORESOURCE_MEM_64) {
  225. if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
  226. && sz64 > 0x100000000ULL) {
  227. res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  228. res->start = 0;
  229. res->end = 0;
  230. pci_err(dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
  231. pos, (unsigned long long)sz64);
  232. goto out;
  233. }
  234. if ((sizeof(pci_bus_addr_t) < 8) && l) {
  235. /* Above 32-bit boundary; try to reallocate */
  236. res->flags |= IORESOURCE_UNSET;
  237. res->start = 0;
  238. res->end = sz64;
  239. pci_info(dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
  240. pos, (unsigned long long)l64);
  241. goto out;
  242. }
  243. }
  244. region.start = l64;
  245. region.end = l64 + sz64;
  246. pcibios_bus_to_resource(dev->bus, res, &region);
  247. pcibios_resource_to_bus(dev->bus, &inverted_region, res);
  248. /*
  249. * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
  250. * the corresponding resource address (the physical address used by
  251. * the CPU. Converting that resource address back to a bus address
  252. * should yield the original BAR value:
  253. *
  254. * resource_to_bus(bus_to_resource(A)) == A
  255. *
  256. * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
  257. * be claimed by the device.
  258. */
  259. if (inverted_region.start != region.start) {
  260. res->flags |= IORESOURCE_UNSET;
  261. res->start = 0;
  262. res->end = region.end - region.start;
  263. pci_info(dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
  264. pos, (unsigned long long)region.start);
  265. }
  266. goto out;
  267. fail:
  268. res->flags = 0;
  269. out:
  270. if (res->flags)
  271. pci_printk(KERN_DEBUG, dev, "reg 0x%x: %pR\n", pos, res);
  272. return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  273. }
  274. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  275. {
  276. unsigned int pos, reg;
  277. if (dev->non_compliant_bars)
  278. return;
  279. /* Per PCIe r4.0, sec 9.3.4.1.11, the VF BARs are all RO Zero */
  280. if (dev->is_virtfn)
  281. return;
  282. for (pos = 0; pos < howmany; pos++) {
  283. struct resource *res = &dev->resource[pos];
  284. reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  285. pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
  286. }
  287. if (rom) {
  288. struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
  289. dev->rom_base_reg = rom;
  290. res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
  291. IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
  292. __pci_read_base(dev, pci_bar_mem32, res, rom);
  293. }
  294. }
  295. static void pci_read_bridge_io(struct pci_bus *child)
  296. {
  297. struct pci_dev *dev = child->self;
  298. u8 io_base_lo, io_limit_lo;
  299. unsigned long io_mask, io_granularity, base, limit;
  300. struct pci_bus_region region;
  301. struct resource *res;
  302. io_mask = PCI_IO_RANGE_MASK;
  303. io_granularity = 0x1000;
  304. if (dev->io_window_1k) {
  305. /* Support 1K I/O space granularity */
  306. io_mask = PCI_IO_1K_RANGE_MASK;
  307. io_granularity = 0x400;
  308. }
  309. res = child->resource[0];
  310. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  311. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  312. base = (io_base_lo & io_mask) << 8;
  313. limit = (io_limit_lo & io_mask) << 8;
  314. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  315. u16 io_base_hi, io_limit_hi;
  316. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  317. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  318. base |= ((unsigned long) io_base_hi << 16);
  319. limit |= ((unsigned long) io_limit_hi << 16);
  320. }
  321. if (base <= limit) {
  322. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  323. region.start = base;
  324. region.end = limit + io_granularity - 1;
  325. pcibios_bus_to_resource(dev->bus, res, &region);
  326. pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
  327. }
  328. }
  329. static void pci_read_bridge_mmio(struct pci_bus *child)
  330. {
  331. struct pci_dev *dev = child->self;
  332. u16 mem_base_lo, mem_limit_lo;
  333. unsigned long base, limit;
  334. struct pci_bus_region region;
  335. struct resource *res;
  336. res = child->resource[1];
  337. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  338. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  339. base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  340. limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  341. if (base <= limit) {
  342. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  343. region.start = base;
  344. region.end = limit + 0xfffff;
  345. pcibios_bus_to_resource(dev->bus, res, &region);
  346. pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
  347. }
  348. }
  349. static void pci_read_bridge_mmio_pref(struct pci_bus *child)
  350. {
  351. struct pci_dev *dev = child->self;
  352. u16 mem_base_lo, mem_limit_lo;
  353. u64 base64, limit64;
  354. pci_bus_addr_t base, limit;
  355. struct pci_bus_region region;
  356. struct resource *res;
  357. res = child->resource[2];
  358. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  359. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  360. base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  361. limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  362. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  363. u32 mem_base_hi, mem_limit_hi;
  364. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  365. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  366. /*
  367. * Some bridges set the base > limit by default, and some
  368. * (broken) BIOSes do not initialize them. If we find
  369. * this, just assume they are not being used.
  370. */
  371. if (mem_base_hi <= mem_limit_hi) {
  372. base64 |= (u64) mem_base_hi << 32;
  373. limit64 |= (u64) mem_limit_hi << 32;
  374. }
  375. }
  376. base = (pci_bus_addr_t) base64;
  377. limit = (pci_bus_addr_t) limit64;
  378. if (base != base64) {
  379. pci_err(dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
  380. (unsigned long long) base64);
  381. return;
  382. }
  383. if (base <= limit) {
  384. res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
  385. IORESOURCE_MEM | IORESOURCE_PREFETCH;
  386. if (res->flags & PCI_PREF_RANGE_TYPE_64)
  387. res->flags |= IORESOURCE_MEM_64;
  388. region.start = base;
  389. region.end = limit + 0xfffff;
  390. pcibios_bus_to_resource(dev->bus, res, &region);
  391. pci_printk(KERN_DEBUG, dev, " bridge window %pR\n", res);
  392. }
  393. }
  394. void pci_read_bridge_bases(struct pci_bus *child)
  395. {
  396. struct pci_dev *dev = child->self;
  397. struct resource *res;
  398. int i;
  399. if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
  400. return;
  401. pci_info(dev, "PCI bridge to %pR%s\n",
  402. &child->busn_res,
  403. dev->transparent ? " (subtractive decode)" : "");
  404. pci_bus_remove_resources(child);
  405. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  406. child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  407. pci_read_bridge_io(child);
  408. pci_read_bridge_mmio(child);
  409. pci_read_bridge_mmio_pref(child);
  410. if (dev->transparent) {
  411. pci_bus_for_each_resource(child->parent, res, i) {
  412. if (res && res->flags) {
  413. pci_bus_add_resource(child, res,
  414. PCI_SUBTRACTIVE_DECODE);
  415. pci_printk(KERN_DEBUG, dev,
  416. " bridge window %pR (subtractive decode)\n",
  417. res);
  418. }
  419. }
  420. }
  421. }
  422. static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
  423. {
  424. struct pci_bus *b;
  425. b = kzalloc(sizeof(*b), GFP_KERNEL);
  426. if (!b)
  427. return NULL;
  428. INIT_LIST_HEAD(&b->node);
  429. INIT_LIST_HEAD(&b->children);
  430. INIT_LIST_HEAD(&b->devices);
  431. INIT_LIST_HEAD(&b->slots);
  432. INIT_LIST_HEAD(&b->resources);
  433. b->max_bus_speed = PCI_SPEED_UNKNOWN;
  434. b->cur_bus_speed = PCI_SPEED_UNKNOWN;
  435. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  436. if (parent)
  437. b->domain_nr = parent->domain_nr;
  438. #endif
  439. return b;
  440. }
  441. static void devm_pci_release_host_bridge_dev(struct device *dev)
  442. {
  443. struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
  444. if (bridge->release_fn)
  445. bridge->release_fn(bridge);
  446. pci_free_resource_list(&bridge->windows);
  447. }
  448. static void pci_release_host_bridge_dev(struct device *dev)
  449. {
  450. devm_pci_release_host_bridge_dev(dev);
  451. kfree(to_pci_host_bridge(dev));
  452. }
  453. struct pci_host_bridge *pci_alloc_host_bridge(size_t priv)
  454. {
  455. struct pci_host_bridge *bridge;
  456. bridge = kzalloc(sizeof(*bridge) + priv, GFP_KERNEL);
  457. if (!bridge)
  458. return NULL;
  459. INIT_LIST_HEAD(&bridge->windows);
  460. bridge->dev.release = pci_release_host_bridge_dev;
  461. /*
  462. * We assume we can manage these PCIe features. Some systems may
  463. * reserve these for use by the platform itself, e.g., an ACPI BIOS
  464. * may implement its own AER handling and use _OSC to prevent the
  465. * OS from interfering.
  466. */
  467. bridge->native_aer = 1;
  468. bridge->native_pcie_hotplug = 1;
  469. bridge->native_shpc_hotplug = 1;
  470. bridge->native_pme = 1;
  471. bridge->native_ltr = 1;
  472. return bridge;
  473. }
  474. EXPORT_SYMBOL(pci_alloc_host_bridge);
  475. struct pci_host_bridge *devm_pci_alloc_host_bridge(struct device *dev,
  476. size_t priv)
  477. {
  478. struct pci_host_bridge *bridge;
  479. bridge = devm_kzalloc(dev, sizeof(*bridge) + priv, GFP_KERNEL);
  480. if (!bridge)
  481. return NULL;
  482. INIT_LIST_HEAD(&bridge->windows);
  483. bridge->dev.release = devm_pci_release_host_bridge_dev;
  484. return bridge;
  485. }
  486. EXPORT_SYMBOL(devm_pci_alloc_host_bridge);
  487. void pci_free_host_bridge(struct pci_host_bridge *bridge)
  488. {
  489. pci_free_resource_list(&bridge->windows);
  490. kfree(bridge);
  491. }
  492. EXPORT_SYMBOL(pci_free_host_bridge);
  493. static const unsigned char pcix_bus_speed[] = {
  494. PCI_SPEED_UNKNOWN, /* 0 */
  495. PCI_SPEED_66MHz_PCIX, /* 1 */
  496. PCI_SPEED_100MHz_PCIX, /* 2 */
  497. PCI_SPEED_133MHz_PCIX, /* 3 */
  498. PCI_SPEED_UNKNOWN, /* 4 */
  499. PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
  500. PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
  501. PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
  502. PCI_SPEED_UNKNOWN, /* 8 */
  503. PCI_SPEED_66MHz_PCIX_266, /* 9 */
  504. PCI_SPEED_100MHz_PCIX_266, /* A */
  505. PCI_SPEED_133MHz_PCIX_266, /* B */
  506. PCI_SPEED_UNKNOWN, /* C */
  507. PCI_SPEED_66MHz_PCIX_533, /* D */
  508. PCI_SPEED_100MHz_PCIX_533, /* E */
  509. PCI_SPEED_133MHz_PCIX_533 /* F */
  510. };
  511. const unsigned char pcie_link_speed[] = {
  512. PCI_SPEED_UNKNOWN, /* 0 */
  513. PCIE_SPEED_2_5GT, /* 1 */
  514. PCIE_SPEED_5_0GT, /* 2 */
  515. PCIE_SPEED_8_0GT, /* 3 */
  516. PCIE_SPEED_16_0GT, /* 4 */
  517. PCI_SPEED_UNKNOWN, /* 5 */
  518. PCI_SPEED_UNKNOWN, /* 6 */
  519. PCI_SPEED_UNKNOWN, /* 7 */
  520. PCI_SPEED_UNKNOWN, /* 8 */
  521. PCI_SPEED_UNKNOWN, /* 9 */
  522. PCI_SPEED_UNKNOWN, /* A */
  523. PCI_SPEED_UNKNOWN, /* B */
  524. PCI_SPEED_UNKNOWN, /* C */
  525. PCI_SPEED_UNKNOWN, /* D */
  526. PCI_SPEED_UNKNOWN, /* E */
  527. PCI_SPEED_UNKNOWN /* F */
  528. };
  529. void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
  530. {
  531. bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
  532. }
  533. EXPORT_SYMBOL_GPL(pcie_update_link_speed);
  534. static unsigned char agp_speeds[] = {
  535. AGP_UNKNOWN,
  536. AGP_1X,
  537. AGP_2X,
  538. AGP_4X,
  539. AGP_8X
  540. };
  541. static enum pci_bus_speed agp_speed(int agp3, int agpstat)
  542. {
  543. int index = 0;
  544. if (agpstat & 4)
  545. index = 3;
  546. else if (agpstat & 2)
  547. index = 2;
  548. else if (agpstat & 1)
  549. index = 1;
  550. else
  551. goto out;
  552. if (agp3) {
  553. index += 2;
  554. if (index == 5)
  555. index = 0;
  556. }
  557. out:
  558. return agp_speeds[index];
  559. }
  560. static void pci_set_bus_speed(struct pci_bus *bus)
  561. {
  562. struct pci_dev *bridge = bus->self;
  563. int pos;
  564. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
  565. if (!pos)
  566. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
  567. if (pos) {
  568. u32 agpstat, agpcmd;
  569. pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
  570. bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
  571. pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
  572. bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
  573. }
  574. pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  575. if (pos) {
  576. u16 status;
  577. enum pci_bus_speed max;
  578. pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
  579. &status);
  580. if (status & PCI_X_SSTATUS_533MHZ) {
  581. max = PCI_SPEED_133MHz_PCIX_533;
  582. } else if (status & PCI_X_SSTATUS_266MHZ) {
  583. max = PCI_SPEED_133MHz_PCIX_266;
  584. } else if (status & PCI_X_SSTATUS_133MHZ) {
  585. if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
  586. max = PCI_SPEED_133MHz_PCIX_ECC;
  587. else
  588. max = PCI_SPEED_133MHz_PCIX;
  589. } else {
  590. max = PCI_SPEED_66MHz_PCIX;
  591. }
  592. bus->max_bus_speed = max;
  593. bus->cur_bus_speed = pcix_bus_speed[
  594. (status & PCI_X_SSTATUS_FREQ) >> 6];
  595. return;
  596. }
  597. if (pci_is_pcie(bridge)) {
  598. u32 linkcap;
  599. u16 linksta;
  600. pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
  601. bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
  602. bridge->link_active_reporting = !!(linkcap & PCI_EXP_LNKCAP_DLLLARC);
  603. pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
  604. pcie_update_link_speed(bus, linksta);
  605. }
  606. }
  607. static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
  608. {
  609. struct irq_domain *d;
  610. /*
  611. * Any firmware interface that can resolve the msi_domain
  612. * should be called from here.
  613. */
  614. d = pci_host_bridge_of_msi_domain(bus);
  615. if (!d)
  616. d = pci_host_bridge_acpi_msi_domain(bus);
  617. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  618. /*
  619. * If no IRQ domain was found via the OF tree, try looking it up
  620. * directly through the fwnode_handle.
  621. */
  622. if (!d) {
  623. struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
  624. if (fwnode)
  625. d = irq_find_matching_fwnode(fwnode,
  626. DOMAIN_BUS_PCI_MSI);
  627. }
  628. #endif
  629. return d;
  630. }
  631. static void pci_set_bus_msi_domain(struct pci_bus *bus)
  632. {
  633. struct irq_domain *d;
  634. struct pci_bus *b;
  635. /*
  636. * The bus can be a root bus, a subordinate bus, or a virtual bus
  637. * created by an SR-IOV device. Walk up to the first bridge device
  638. * found or derive the domain from the host bridge.
  639. */
  640. for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
  641. if (b->self)
  642. d = dev_get_msi_domain(&b->self->dev);
  643. }
  644. if (!d)
  645. d = pci_host_bridge_msi_domain(b);
  646. dev_set_msi_domain(&bus->dev, d);
  647. }
  648. static int pci_register_host_bridge(struct pci_host_bridge *bridge)
  649. {
  650. struct device *parent = bridge->dev.parent;
  651. struct resource_entry *window, *n;
  652. struct pci_bus *bus, *b;
  653. resource_size_t offset;
  654. LIST_HEAD(resources);
  655. struct resource *res;
  656. char addr[64], *fmt;
  657. const char *name;
  658. int err;
  659. bus = pci_alloc_bus(NULL);
  660. if (!bus)
  661. return -ENOMEM;
  662. bridge->bus = bus;
  663. /* Temporarily move resources off the list */
  664. list_splice_init(&bridge->windows, &resources);
  665. bus->sysdata = bridge->sysdata;
  666. bus->msi = bridge->msi;
  667. bus->ops = bridge->ops;
  668. bus->number = bus->busn_res.start = bridge->busnr;
  669. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  670. bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
  671. #endif
  672. b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
  673. if (b) {
  674. /* Ignore it if we already got here via a different bridge */
  675. dev_dbg(&b->dev, "bus already known\n");
  676. err = -EEXIST;
  677. goto free;
  678. }
  679. dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(bus),
  680. bridge->busnr);
  681. err = pcibios_root_bridge_prepare(bridge);
  682. if (err)
  683. goto free;
  684. err = device_register(&bridge->dev);
  685. if (err)
  686. put_device(&bridge->dev);
  687. bus->bridge = get_device(&bridge->dev);
  688. device_enable_async_suspend(bus->bridge);
  689. pci_set_bus_of_node(bus);
  690. pci_set_bus_msi_domain(bus);
  691. if (!parent)
  692. set_dev_node(bus->bridge, pcibus_to_node(bus));
  693. bus->dev.class = &pcibus_class;
  694. bus->dev.parent = bus->bridge;
  695. dev_set_name(&bus->dev, "%04x:%02x", pci_domain_nr(bus), bus->number);
  696. name = dev_name(&bus->dev);
  697. err = device_register(&bus->dev);
  698. if (err)
  699. goto unregister;
  700. pcibios_add_bus(bus);
  701. /* Create legacy_io and legacy_mem files for this bus */
  702. pci_create_legacy_files(bus);
  703. if (parent)
  704. dev_info(parent, "PCI host bridge to bus %s\n", name);
  705. else
  706. pr_info("PCI host bridge to bus %s\n", name);
  707. /* Add initial resources to the bus */
  708. resource_list_for_each_entry_safe(window, n, &resources) {
  709. list_move_tail(&window->node, &bridge->windows);
  710. offset = window->offset;
  711. res = window->res;
  712. if (res->flags & IORESOURCE_BUS)
  713. pci_bus_insert_busn_res(bus, bus->number, res->end);
  714. else
  715. pci_bus_add_resource(bus, res, 0);
  716. if (offset) {
  717. if (resource_type(res) == IORESOURCE_IO)
  718. fmt = " (bus address [%#06llx-%#06llx])";
  719. else
  720. fmt = " (bus address [%#010llx-%#010llx])";
  721. snprintf(addr, sizeof(addr), fmt,
  722. (unsigned long long)(res->start - offset),
  723. (unsigned long long)(res->end - offset));
  724. } else
  725. addr[0] = '\0';
  726. dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr);
  727. }
  728. down_write(&pci_bus_sem);
  729. list_add_tail(&bus->node, &pci_root_buses);
  730. up_write(&pci_bus_sem);
  731. return 0;
  732. unregister:
  733. put_device(&bridge->dev);
  734. device_unregister(&bridge->dev);
  735. free:
  736. kfree(bus);
  737. return err;
  738. }
  739. static bool pci_bridge_child_ext_cfg_accessible(struct pci_dev *bridge)
  740. {
  741. int pos;
  742. u32 status;
  743. /*
  744. * If extended config space isn't accessible on a bridge's primary
  745. * bus, we certainly can't access it on the secondary bus.
  746. */
  747. if (bridge->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
  748. return false;
  749. /*
  750. * PCIe Root Ports and switch ports are PCIe on both sides, so if
  751. * extended config space is accessible on the primary, it's also
  752. * accessible on the secondary.
  753. */
  754. if (pci_is_pcie(bridge) &&
  755. (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT ||
  756. pci_pcie_type(bridge) == PCI_EXP_TYPE_UPSTREAM ||
  757. pci_pcie_type(bridge) == PCI_EXP_TYPE_DOWNSTREAM))
  758. return true;
  759. /*
  760. * For the other bridge types:
  761. * - PCI-to-PCI bridges
  762. * - PCIe-to-PCI/PCI-X forward bridges
  763. * - PCI/PCI-X-to-PCIe reverse bridges
  764. * extended config space on the secondary side is only accessible
  765. * if the bridge supports PCI-X Mode 2.
  766. */
  767. pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  768. if (!pos)
  769. return false;
  770. pci_read_config_dword(bridge, pos + PCI_X_STATUS, &status);
  771. return status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ);
  772. }
  773. static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  774. struct pci_dev *bridge, int busnr)
  775. {
  776. struct pci_bus *child;
  777. int i;
  778. int ret;
  779. /* Allocate a new bus and inherit stuff from the parent */
  780. child = pci_alloc_bus(parent);
  781. if (!child)
  782. return NULL;
  783. child->parent = parent;
  784. child->ops = parent->ops;
  785. child->msi = parent->msi;
  786. child->sysdata = parent->sysdata;
  787. child->bus_flags = parent->bus_flags;
  788. /*
  789. * Initialize some portions of the bus device, but don't register
  790. * it now as the parent is not properly set up yet.
  791. */
  792. child->dev.class = &pcibus_class;
  793. dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
  794. /* Set up the primary, secondary and subordinate bus numbers */
  795. child->number = child->busn_res.start = busnr;
  796. child->primary = parent->busn_res.start;
  797. child->busn_res.end = 0xff;
  798. if (!bridge) {
  799. child->dev.parent = parent->bridge;
  800. goto add_dev;
  801. }
  802. child->self = bridge;
  803. child->bridge = get_device(&bridge->dev);
  804. child->dev.parent = child->bridge;
  805. pci_set_bus_of_node(child);
  806. pci_set_bus_speed(child);
  807. /*
  808. * Check whether extended config space is accessible on the child
  809. * bus. Note that we currently assume it is always accessible on
  810. * the root bus.
  811. */
  812. if (!pci_bridge_child_ext_cfg_accessible(bridge)) {
  813. child->bus_flags |= PCI_BUS_FLAGS_NO_EXTCFG;
  814. pci_info(child, "extended config space not accessible\n");
  815. }
  816. /* Set up default resource pointers and names */
  817. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  818. child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  819. child->resource[i]->name = child->name;
  820. }
  821. bridge->subordinate = child;
  822. add_dev:
  823. pci_set_bus_msi_domain(child);
  824. ret = device_register(&child->dev);
  825. WARN_ON(ret < 0);
  826. pcibios_add_bus(child);
  827. if (child->ops->add_bus) {
  828. ret = child->ops->add_bus(child);
  829. if (WARN_ON(ret < 0))
  830. dev_err(&child->dev, "failed to add bus: %d\n", ret);
  831. }
  832. /* Create legacy_io and legacy_mem files for this bus */
  833. pci_create_legacy_files(child);
  834. return child;
  835. }
  836. struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
  837. int busnr)
  838. {
  839. struct pci_bus *child;
  840. child = pci_alloc_child_bus(parent, dev, busnr);
  841. if (child) {
  842. down_write(&pci_bus_sem);
  843. list_add_tail(&child->node, &parent->children);
  844. up_write(&pci_bus_sem);
  845. }
  846. return child;
  847. }
  848. EXPORT_SYMBOL(pci_add_new_bus);
  849. static void pci_enable_crs(struct pci_dev *pdev)
  850. {
  851. u16 root_cap = 0;
  852. /* Enable CRS Software Visibility if supported */
  853. pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
  854. if (root_cap & PCI_EXP_RTCAP_CRSVIS)
  855. pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
  856. PCI_EXP_RTCTL_CRSSVE);
  857. }
  858. static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
  859. unsigned int available_buses);
  860. /*
  861. * pci_scan_bridge_extend() - Scan buses behind a bridge
  862. * @bus: Parent bus the bridge is on
  863. * @dev: Bridge itself
  864. * @max: Starting subordinate number of buses behind this bridge
  865. * @available_buses: Total number of buses available for this bridge and
  866. * the devices below. After the minimal bus space has
  867. * been allocated the remaining buses will be
  868. * distributed equally between hotplug-capable bridges.
  869. * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
  870. * that need to be reconfigured.
  871. *
  872. * If it's a bridge, configure it and scan the bus behind it.
  873. * For CardBus bridges, we don't scan behind as the devices will
  874. * be handled by the bridge driver itself.
  875. *
  876. * We need to process bridges in two passes -- first we scan those
  877. * already configured by the BIOS and after we are done with all of
  878. * them, we proceed to assigning numbers to the remaining buses in
  879. * order to avoid overlaps between old and new bus numbers.
  880. *
  881. * Return: New subordinate number covering all buses behind this bridge.
  882. */
  883. static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
  884. int max, unsigned int available_buses,
  885. int pass)
  886. {
  887. struct pci_bus *child;
  888. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  889. u32 buses, i, j = 0;
  890. u16 bctl;
  891. u8 primary, secondary, subordinate;
  892. int broken = 0;
  893. /*
  894. * Make sure the bridge is powered on to be able to access config
  895. * space of devices below it.
  896. */
  897. pm_runtime_get_sync(&dev->dev);
  898. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  899. primary = buses & 0xFF;
  900. secondary = (buses >> 8) & 0xFF;
  901. subordinate = (buses >> 16) & 0xFF;
  902. pci_dbg(dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
  903. secondary, subordinate, pass);
  904. if (!primary && (primary != bus->number) && secondary && subordinate) {
  905. pci_warn(dev, "Primary bus is hard wired to 0\n");
  906. primary = bus->number;
  907. }
  908. /* Check if setup is sensible at all */
  909. if (!pass &&
  910. (primary != bus->number || secondary <= bus->number ||
  911. secondary > subordinate)) {
  912. pci_info(dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
  913. secondary, subordinate);
  914. broken = 1;
  915. }
  916. /*
  917. * Disable Master-Abort Mode during probing to avoid reporting of
  918. * bus errors in some architectures.
  919. */
  920. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  921. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  922. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  923. pci_enable_crs(dev);
  924. if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
  925. !is_cardbus && !broken) {
  926. unsigned int cmax;
  927. /*
  928. * Bus already configured by firmware, process it in the
  929. * first pass and just note the configuration.
  930. */
  931. if (pass)
  932. goto out;
  933. /*
  934. * The bus might already exist for two reasons: Either we
  935. * are rescanning the bus or the bus is reachable through
  936. * more than one bridge. The second case can happen with
  937. * the i450NX chipset.
  938. */
  939. child = pci_find_bus(pci_domain_nr(bus), secondary);
  940. if (!child) {
  941. child = pci_add_new_bus(bus, dev, secondary);
  942. if (!child)
  943. goto out;
  944. child->primary = primary;
  945. pci_bus_insert_busn_res(child, secondary, subordinate);
  946. child->bridge_ctl = bctl;
  947. }
  948. cmax = pci_scan_child_bus(child);
  949. if (cmax > subordinate)
  950. pci_warn(dev, "bridge has subordinate %02x but max busn %02x\n",
  951. subordinate, cmax);
  952. /* Subordinate should equal child->busn_res.end */
  953. if (subordinate > max)
  954. max = subordinate;
  955. } else {
  956. /*
  957. * We need to assign a number to this bus which we always
  958. * do in the second pass.
  959. */
  960. if (!pass) {
  961. if (pcibios_assign_all_busses() || broken || is_cardbus)
  962. /*
  963. * Temporarily disable forwarding of the
  964. * configuration cycles on all bridges in
  965. * this bus segment to avoid possible
  966. * conflicts in the second pass between two
  967. * bridges programmed with overlapping bus
  968. * ranges.
  969. */
  970. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  971. buses & ~0xffffff);
  972. goto out;
  973. }
  974. /* Clear errors */
  975. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  976. /*
  977. * Prevent assigning a bus number that already exists.
  978. * This can happen when a bridge is hot-plugged, so in this
  979. * case we only re-scan this bus.
  980. */
  981. child = pci_find_bus(pci_domain_nr(bus), max+1);
  982. if (!child) {
  983. child = pci_add_new_bus(bus, dev, max+1);
  984. if (!child)
  985. goto out;
  986. pci_bus_insert_busn_res(child, max+1,
  987. bus->busn_res.end);
  988. }
  989. max++;
  990. if (available_buses)
  991. available_buses--;
  992. buses = (buses & 0xff000000)
  993. | ((unsigned int)(child->primary) << 0)
  994. | ((unsigned int)(child->busn_res.start) << 8)
  995. | ((unsigned int)(child->busn_res.end) << 16);
  996. /*
  997. * yenta.c forces a secondary latency timer of 176.
  998. * Copy that behaviour here.
  999. */
  1000. if (is_cardbus) {
  1001. buses &= ~0xff000000;
  1002. buses |= CARDBUS_LATENCY_TIMER << 24;
  1003. }
  1004. /* We need to blast all three values with a single write */
  1005. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  1006. if (!is_cardbus) {
  1007. child->bridge_ctl = bctl;
  1008. max = pci_scan_child_bus_extend(child, available_buses);
  1009. } else {
  1010. /*
  1011. * For CardBus bridges, we leave 4 bus numbers as
  1012. * cards with a PCI-to-PCI bridge can be inserted
  1013. * later.
  1014. */
  1015. for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
  1016. struct pci_bus *parent = bus;
  1017. if (pci_find_bus(pci_domain_nr(bus),
  1018. max+i+1))
  1019. break;
  1020. while (parent->parent) {
  1021. if ((!pcibios_assign_all_busses()) &&
  1022. (parent->busn_res.end > max) &&
  1023. (parent->busn_res.end <= max+i)) {
  1024. j = 1;
  1025. }
  1026. parent = parent->parent;
  1027. }
  1028. if (j) {
  1029. /*
  1030. * Often, there are two CardBus
  1031. * bridges -- try to leave one
  1032. * valid bus number for each one.
  1033. */
  1034. i /= 2;
  1035. break;
  1036. }
  1037. }
  1038. max += i;
  1039. }
  1040. /* Set subordinate bus number to its real value */
  1041. pci_bus_update_busn_res_end(child, max);
  1042. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  1043. }
  1044. sprintf(child->name,
  1045. (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
  1046. pci_domain_nr(bus), child->number);
  1047. /* Check that all devices are accessible */
  1048. while (bus->parent) {
  1049. if ((child->busn_res.end > bus->busn_res.end) ||
  1050. (child->number > bus->busn_res.end) ||
  1051. (child->number < bus->number) ||
  1052. (child->busn_res.end < bus->number)) {
  1053. dev_info(&dev->dev, "devices behind bridge are unusable because %pR cannot be assigned for them\n",
  1054. &child->busn_res);
  1055. break;
  1056. }
  1057. bus = bus->parent;
  1058. }
  1059. out:
  1060. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  1061. pm_runtime_put(&dev->dev);
  1062. return max;
  1063. }
  1064. /*
  1065. * pci_scan_bridge() - Scan buses behind a bridge
  1066. * @bus: Parent bus the bridge is on
  1067. * @dev: Bridge itself
  1068. * @max: Starting subordinate number of buses behind this bridge
  1069. * @pass: Either %0 (scan already configured bridges) or %1 (scan bridges
  1070. * that need to be reconfigured.
  1071. *
  1072. * If it's a bridge, configure it and scan the bus behind it.
  1073. * For CardBus bridges, we don't scan behind as the devices will
  1074. * be handled by the bridge driver itself.
  1075. *
  1076. * We need to process bridges in two passes -- first we scan those
  1077. * already configured by the BIOS and after we are done with all of
  1078. * them, we proceed to assigning numbers to the remaining buses in
  1079. * order to avoid overlaps between old and new bus numbers.
  1080. *
  1081. * Return: New subordinate number covering all buses behind this bridge.
  1082. */
  1083. int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
  1084. {
  1085. return pci_scan_bridge_extend(bus, dev, max, 0, pass);
  1086. }
  1087. EXPORT_SYMBOL(pci_scan_bridge);
  1088. /*
  1089. * Read interrupt line and base address registers.
  1090. * The architecture-dependent code can tweak these, of course.
  1091. */
  1092. static void pci_read_irq(struct pci_dev *dev)
  1093. {
  1094. unsigned char irq;
  1095. /* VFs are not allowed to use INTx, so skip the config reads */
  1096. if (dev->is_virtfn) {
  1097. dev->pin = 0;
  1098. dev->irq = 0;
  1099. return;
  1100. }
  1101. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  1102. dev->pin = irq;
  1103. if (irq)
  1104. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  1105. dev->irq = irq;
  1106. }
  1107. void set_pcie_port_type(struct pci_dev *pdev)
  1108. {
  1109. int pos;
  1110. u16 reg16;
  1111. int type;
  1112. struct pci_dev *parent;
  1113. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  1114. if (!pos)
  1115. return;
  1116. pdev->pcie_cap = pos;
  1117. pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  1118. pdev->pcie_flags_reg = reg16;
  1119. pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
  1120. pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
  1121. /*
  1122. * A Root Port or a PCI-to-PCIe bridge is always the upstream end
  1123. * of a Link. No PCIe component has two Links. Two Links are
  1124. * connected by a Switch that has a Port on each Link and internal
  1125. * logic to connect the two Ports.
  1126. */
  1127. type = pci_pcie_type(pdev);
  1128. if (type == PCI_EXP_TYPE_ROOT_PORT ||
  1129. type == PCI_EXP_TYPE_PCIE_BRIDGE)
  1130. pdev->has_secondary_link = 1;
  1131. else if (type == PCI_EXP_TYPE_UPSTREAM ||
  1132. type == PCI_EXP_TYPE_DOWNSTREAM) {
  1133. parent = pci_upstream_bridge(pdev);
  1134. /*
  1135. * Usually there's an upstream device (Root Port or Switch
  1136. * Downstream Port), but we can't assume one exists.
  1137. */
  1138. if (parent && !parent->has_secondary_link)
  1139. pdev->has_secondary_link = 1;
  1140. }
  1141. }
  1142. void set_pcie_hotplug_bridge(struct pci_dev *pdev)
  1143. {
  1144. u32 reg32;
  1145. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
  1146. if (reg32 & PCI_EXP_SLTCAP_HPC)
  1147. pdev->is_hotplug_bridge = 1;
  1148. }
  1149. static void set_pcie_thunderbolt(struct pci_dev *dev)
  1150. {
  1151. int vsec = 0;
  1152. u32 header;
  1153. while ((vsec = pci_find_next_ext_capability(dev, vsec,
  1154. PCI_EXT_CAP_ID_VNDR))) {
  1155. pci_read_config_dword(dev, vsec + PCI_VNDR_HEADER, &header);
  1156. /* Is the device part of a Thunderbolt controller? */
  1157. if (dev->vendor == PCI_VENDOR_ID_INTEL &&
  1158. PCI_VNDR_HEADER_ID(header) == PCI_VSEC_ID_INTEL_TBT) {
  1159. dev->is_thunderbolt = 1;
  1160. return;
  1161. }
  1162. }
  1163. }
  1164. /**
  1165. * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
  1166. * @dev: PCI device
  1167. *
  1168. * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
  1169. * when forwarding a type1 configuration request the bridge must check that
  1170. * the extended register address field is zero. The bridge is not permitted
  1171. * to forward the transactions and must handle it as an Unsupported Request.
  1172. * Some bridges do not follow this rule and simply drop the extended register
  1173. * bits, resulting in the standard config space being aliased, every 256
  1174. * bytes across the entire configuration space. Test for this condition by
  1175. * comparing the first dword of each potential alias to the vendor/device ID.
  1176. * Known offenders:
  1177. * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
  1178. * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
  1179. */
  1180. static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
  1181. {
  1182. #ifdef CONFIG_PCI_QUIRKS
  1183. int pos;
  1184. u32 header, tmp;
  1185. pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
  1186. for (pos = PCI_CFG_SPACE_SIZE;
  1187. pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
  1188. if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
  1189. || header != tmp)
  1190. return false;
  1191. }
  1192. return true;
  1193. #else
  1194. return false;
  1195. #endif
  1196. }
  1197. /**
  1198. * pci_cfg_space_size - Get the configuration space size of the PCI device
  1199. * @dev: PCI device
  1200. *
  1201. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  1202. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  1203. * access it. Maybe we don't have a way to generate extended config space
  1204. * accesses, or the device is behind a reverse Express bridge. So we try
  1205. * reading the dword at 0x100 which must either be 0 or a valid extended
  1206. * capability header.
  1207. */
  1208. static int pci_cfg_space_size_ext(struct pci_dev *dev)
  1209. {
  1210. u32 status;
  1211. int pos = PCI_CFG_SPACE_SIZE;
  1212. if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
  1213. return PCI_CFG_SPACE_SIZE;
  1214. if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
  1215. return PCI_CFG_SPACE_SIZE;
  1216. return PCI_CFG_SPACE_EXP_SIZE;
  1217. }
  1218. #ifdef CONFIG_PCI_IOV
  1219. static bool is_vf0(struct pci_dev *dev)
  1220. {
  1221. if (pci_iov_virtfn_devfn(dev->physfn, 0) == dev->devfn &&
  1222. pci_iov_virtfn_bus(dev->physfn, 0) == dev->bus->number)
  1223. return true;
  1224. return false;
  1225. }
  1226. #endif
  1227. int pci_cfg_space_size(struct pci_dev *dev)
  1228. {
  1229. int pos;
  1230. u32 status;
  1231. u16 class;
  1232. #ifdef CONFIG_PCI_IOV
  1233. /* Read cached value for all VFs except for VF0 */
  1234. if (dev->is_virtfn && !is_vf0(dev))
  1235. return dev->physfn->sriov->cfg_size;
  1236. #endif
  1237. if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_EXTCFG)
  1238. return PCI_CFG_SPACE_SIZE;
  1239. class = dev->class >> 8;
  1240. if (class == PCI_CLASS_BRIDGE_HOST)
  1241. return pci_cfg_space_size_ext(dev);
  1242. if (pci_is_pcie(dev))
  1243. return pci_cfg_space_size_ext(dev);
  1244. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1245. if (!pos)
  1246. return PCI_CFG_SPACE_SIZE;
  1247. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  1248. if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
  1249. return pci_cfg_space_size_ext(dev);
  1250. return PCI_CFG_SPACE_SIZE;
  1251. }
  1252. static u32 pci_class(struct pci_dev *dev)
  1253. {
  1254. u32 class;
  1255. #ifdef CONFIG_PCI_IOV
  1256. if (dev->is_virtfn)
  1257. return dev->physfn->sriov->class;
  1258. #endif
  1259. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  1260. return class;
  1261. }
  1262. static void pci_subsystem_ids(struct pci_dev *dev, u16 *vendor, u16 *device)
  1263. {
  1264. #ifdef CONFIG_PCI_IOV
  1265. if (dev->is_virtfn) {
  1266. *vendor = dev->physfn->sriov->subsystem_vendor;
  1267. *device = dev->physfn->sriov->subsystem_device;
  1268. return;
  1269. }
  1270. #endif
  1271. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, vendor);
  1272. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, device);
  1273. }
  1274. static u8 pci_hdr_type(struct pci_dev *dev)
  1275. {
  1276. u8 hdr_type;
  1277. #ifdef CONFIG_PCI_IOV
  1278. if (dev->is_virtfn)
  1279. return dev->physfn->sriov->hdr_type;
  1280. #endif
  1281. pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
  1282. return hdr_type;
  1283. }
  1284. #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  1285. static void pci_msi_setup_pci_dev(struct pci_dev *dev)
  1286. {
  1287. /*
  1288. * Disable the MSI hardware to avoid screaming interrupts
  1289. * during boot. This is the power on reset default so
  1290. * usually this should be a noop.
  1291. */
  1292. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1293. if (dev->msi_cap)
  1294. pci_msi_set_enable(dev, 0);
  1295. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1296. if (dev->msix_cap)
  1297. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  1298. }
  1299. /**
  1300. * pci_intx_mask_broken - Test PCI_COMMAND_INTX_DISABLE writability
  1301. * @dev: PCI device
  1302. *
  1303. * Test whether PCI_COMMAND_INTX_DISABLE is writable for @dev. Check this
  1304. * at enumeration-time to avoid modifying PCI_COMMAND at run-time.
  1305. */
  1306. static int pci_intx_mask_broken(struct pci_dev *dev)
  1307. {
  1308. u16 orig, toggle, new;
  1309. pci_read_config_word(dev, PCI_COMMAND, &orig);
  1310. toggle = orig ^ PCI_COMMAND_INTX_DISABLE;
  1311. pci_write_config_word(dev, PCI_COMMAND, toggle);
  1312. pci_read_config_word(dev, PCI_COMMAND, &new);
  1313. pci_write_config_word(dev, PCI_COMMAND, orig);
  1314. /*
  1315. * PCI_COMMAND_INTX_DISABLE was reserved and read-only prior to PCI
  1316. * r2.3, so strictly speaking, a device is not *broken* if it's not
  1317. * writable. But we'll live with the misnomer for now.
  1318. */
  1319. if (new != toggle)
  1320. return 1;
  1321. return 0;
  1322. }
  1323. static void early_dump_pci_device(struct pci_dev *pdev)
  1324. {
  1325. u32 value[256 / 4];
  1326. int i;
  1327. pci_info(pdev, "config space:\n");
  1328. for (i = 0; i < 256; i += 4)
  1329. pci_read_config_dword(pdev, i, &value[i / 4]);
  1330. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
  1331. value, 256, false);
  1332. }
  1333. /**
  1334. * pci_setup_device - Fill in class and map information of a device
  1335. * @dev: the device structure to fill
  1336. *
  1337. * Initialize the device structure with information about the device's
  1338. * vendor,class,memory and IO-space addresses, IRQ lines etc.
  1339. * Called at initialisation of the PCI subsystem and by CardBus services.
  1340. * Returns 0 on success and negative if unknown type of device (not normal,
  1341. * bridge or CardBus).
  1342. */
  1343. int pci_setup_device(struct pci_dev *dev)
  1344. {
  1345. u32 class;
  1346. u16 cmd;
  1347. u8 hdr_type;
  1348. int pos = 0;
  1349. struct pci_bus_region region;
  1350. struct resource *res;
  1351. hdr_type = pci_hdr_type(dev);
  1352. dev->sysdata = dev->bus->sysdata;
  1353. dev->dev.parent = dev->bus->bridge;
  1354. dev->dev.bus = &pci_bus_type;
  1355. dev->hdr_type = hdr_type & 0x7f;
  1356. dev->multifunction = !!(hdr_type & 0x80);
  1357. dev->error_state = pci_channel_io_normal;
  1358. set_pcie_port_type(dev);
  1359. pci_dev_assign_slot(dev);
  1360. /*
  1361. * Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  1362. * set this higher, assuming the system even supports it.
  1363. */
  1364. dev->dma_mask = 0xffffffff;
  1365. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  1366. dev->bus->number, PCI_SLOT(dev->devfn),
  1367. PCI_FUNC(dev->devfn));
  1368. class = pci_class(dev);
  1369. dev->revision = class & 0xff;
  1370. dev->class = class >> 8; /* upper 3 bytes */
  1371. pci_printk(KERN_DEBUG, dev, "[%04x:%04x] type %02x class %#08x\n",
  1372. dev->vendor, dev->device, dev->hdr_type, dev->class);
  1373. if (pci_early_dump)
  1374. early_dump_pci_device(dev);
  1375. /* Need to have dev->class ready */
  1376. dev->cfg_size = pci_cfg_space_size(dev);
  1377. /* Need to have dev->cfg_size ready */
  1378. set_pcie_thunderbolt(dev);
  1379. /* "Unknown power state" */
  1380. dev->current_state = PCI_UNKNOWN;
  1381. /* Early fixups, before probing the BARs */
  1382. pci_fixup_device(pci_fixup_early, dev);
  1383. /* Device class may be changed after fixup */
  1384. class = dev->class >> 8;
  1385. if (dev->non_compliant_bars) {
  1386. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1387. if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
  1388. pci_info(dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
  1389. cmd &= ~PCI_COMMAND_IO;
  1390. cmd &= ~PCI_COMMAND_MEMORY;
  1391. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1392. }
  1393. }
  1394. dev->broken_intx_masking = pci_intx_mask_broken(dev);
  1395. switch (dev->hdr_type) { /* header type */
  1396. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  1397. if (class == PCI_CLASS_BRIDGE_PCI)
  1398. goto bad;
  1399. pci_read_irq(dev);
  1400. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  1401. pci_subsystem_ids(dev, &dev->subsystem_vendor, &dev->subsystem_device);
  1402. /*
  1403. * Do the ugly legacy mode stuff here rather than broken chip
  1404. * quirk code. Legacy mode ATA controllers have fixed
  1405. * addresses. These are not always echoed in BAR0-3, and
  1406. * BAR0-3 in a few cases contain junk!
  1407. */
  1408. if (class == PCI_CLASS_STORAGE_IDE) {
  1409. u8 progif;
  1410. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1411. if ((progif & 1) == 0) {
  1412. region.start = 0x1F0;
  1413. region.end = 0x1F7;
  1414. res = &dev->resource[0];
  1415. res->flags = LEGACY_IO_RESOURCE;
  1416. pcibios_bus_to_resource(dev->bus, res, &region);
  1417. pci_info(dev, "legacy IDE quirk: reg 0x10: %pR\n",
  1418. res);
  1419. region.start = 0x3F6;
  1420. region.end = 0x3F6;
  1421. res = &dev->resource[1];
  1422. res->flags = LEGACY_IO_RESOURCE;
  1423. pcibios_bus_to_resource(dev->bus, res, &region);
  1424. pci_info(dev, "legacy IDE quirk: reg 0x14: %pR\n",
  1425. res);
  1426. }
  1427. if ((progif & 4) == 0) {
  1428. region.start = 0x170;
  1429. region.end = 0x177;
  1430. res = &dev->resource[2];
  1431. res->flags = LEGACY_IO_RESOURCE;
  1432. pcibios_bus_to_resource(dev->bus, res, &region);
  1433. pci_info(dev, "legacy IDE quirk: reg 0x18: %pR\n",
  1434. res);
  1435. region.start = 0x376;
  1436. region.end = 0x376;
  1437. res = &dev->resource[3];
  1438. res->flags = LEGACY_IO_RESOURCE;
  1439. pcibios_bus_to_resource(dev->bus, res, &region);
  1440. pci_info(dev, "legacy IDE quirk: reg 0x1c: %pR\n",
  1441. res);
  1442. }
  1443. }
  1444. break;
  1445. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  1446. if (class != PCI_CLASS_BRIDGE_PCI)
  1447. goto bad;
  1448. /*
  1449. * The PCI-to-PCI bridge spec requires that subtractive
  1450. * decoding (i.e. transparent) bridge must have programming
  1451. * interface code of 0x01.
  1452. */
  1453. pci_read_irq(dev);
  1454. dev->transparent = ((dev->class & 0xff) == 1);
  1455. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  1456. set_pcie_hotplug_bridge(dev);
  1457. pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
  1458. if (pos) {
  1459. pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
  1460. pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
  1461. }
  1462. break;
  1463. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  1464. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  1465. goto bad;
  1466. pci_read_irq(dev);
  1467. pci_read_bases(dev, 1, 0);
  1468. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1469. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  1470. break;
  1471. default: /* unknown header */
  1472. pci_err(dev, "unknown header type %02x, ignoring device\n",
  1473. dev->hdr_type);
  1474. return -EIO;
  1475. bad:
  1476. pci_err(dev, "ignoring class %#08x (doesn't match header type %02x)\n",
  1477. dev->class, dev->hdr_type);
  1478. dev->class = PCI_CLASS_NOT_DEFINED << 8;
  1479. }
  1480. /* We found a fine healthy device, go go go... */
  1481. return 0;
  1482. }
  1483. static void pci_configure_mps(struct pci_dev *dev)
  1484. {
  1485. struct pci_dev *bridge = pci_upstream_bridge(dev);
  1486. int mps, mpss, p_mps, rc;
  1487. if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
  1488. return;
  1489. /* MPS and MRRS fields are of type 'RsvdP' for VFs, short-circuit out */
  1490. if (dev->is_virtfn)
  1491. return;
  1492. mps = pcie_get_mps(dev);
  1493. p_mps = pcie_get_mps(bridge);
  1494. if (mps == p_mps)
  1495. return;
  1496. if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
  1497. pci_warn(dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1498. mps, pci_name(bridge), p_mps);
  1499. return;
  1500. }
  1501. /*
  1502. * Fancier MPS configuration is done later by
  1503. * pcie_bus_configure_settings()
  1504. */
  1505. if (pcie_bus_config != PCIE_BUS_DEFAULT)
  1506. return;
  1507. mpss = 128 << dev->pcie_mpss;
  1508. if (mpss < p_mps && pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) {
  1509. pcie_set_mps(bridge, mpss);
  1510. pci_info(dev, "Upstream bridge's Max Payload Size set to %d (was %d, max %d)\n",
  1511. mpss, p_mps, 128 << bridge->pcie_mpss);
  1512. p_mps = pcie_get_mps(bridge);
  1513. }
  1514. rc = pcie_set_mps(dev, p_mps);
  1515. if (rc) {
  1516. pci_warn(dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1517. p_mps);
  1518. return;
  1519. }
  1520. pci_info(dev, "Max Payload Size set to %d (was %d, max %d)\n",
  1521. p_mps, mps, mpss);
  1522. }
  1523. static struct hpp_type0 pci_default_type0 = {
  1524. .revision = 1,
  1525. .cache_line_size = 8,
  1526. .latency_timer = 0x40,
  1527. .enable_serr = 0,
  1528. .enable_perr = 0,
  1529. };
  1530. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  1531. {
  1532. u16 pci_cmd, pci_bctl;
  1533. if (!hpp)
  1534. hpp = &pci_default_type0;
  1535. if (hpp->revision > 1) {
  1536. pci_warn(dev, "PCI settings rev %d not supported; using defaults\n",
  1537. hpp->revision);
  1538. hpp = &pci_default_type0;
  1539. }
  1540. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  1541. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  1542. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1543. if (hpp->enable_serr)
  1544. pci_cmd |= PCI_COMMAND_SERR;
  1545. if (hpp->enable_perr)
  1546. pci_cmd |= PCI_COMMAND_PARITY;
  1547. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1548. /* Program bridge control value */
  1549. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1550. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1551. hpp->latency_timer);
  1552. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1553. if (hpp->enable_serr)
  1554. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1555. if (hpp->enable_perr)
  1556. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1557. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1558. }
  1559. }
  1560. static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
  1561. {
  1562. int pos;
  1563. if (!hpp)
  1564. return;
  1565. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1566. if (!pos)
  1567. return;
  1568. pci_warn(dev, "PCI-X settings not supported\n");
  1569. }
  1570. static bool pcie_root_rcb_set(struct pci_dev *dev)
  1571. {
  1572. struct pci_dev *rp = pcie_find_root_port(dev);
  1573. u16 lnkctl;
  1574. if (!rp)
  1575. return false;
  1576. pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
  1577. if (lnkctl & PCI_EXP_LNKCTL_RCB)
  1578. return true;
  1579. return false;
  1580. }
  1581. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  1582. {
  1583. int pos;
  1584. u32 reg32;
  1585. if (!hpp)
  1586. return;
  1587. if (!pci_is_pcie(dev))
  1588. return;
  1589. if (hpp->revision > 1) {
  1590. pci_warn(dev, "PCIe settings rev %d not supported\n",
  1591. hpp->revision);
  1592. return;
  1593. }
  1594. /*
  1595. * Don't allow _HPX to change MPS or MRRS settings. We manage
  1596. * those to make sure they're consistent with the rest of the
  1597. * platform.
  1598. */
  1599. hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
  1600. PCI_EXP_DEVCTL_READRQ;
  1601. hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
  1602. PCI_EXP_DEVCTL_READRQ);
  1603. /* Initialize Device Control Register */
  1604. pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  1605. ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
  1606. /* Initialize Link Control Register */
  1607. if (pcie_cap_has_lnkctl(dev)) {
  1608. /*
  1609. * If the Root Port supports Read Completion Boundary of
  1610. * 128, set RCB to 128. Otherwise, clear it.
  1611. */
  1612. hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
  1613. hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
  1614. if (pcie_root_rcb_set(dev))
  1615. hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
  1616. pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
  1617. ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
  1618. }
  1619. /* Find Advanced Error Reporting Enhanced Capability */
  1620. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  1621. if (!pos)
  1622. return;
  1623. /* Initialize Uncorrectable Error Mask Register */
  1624. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  1625. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  1626. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  1627. /* Initialize Uncorrectable Error Severity Register */
  1628. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  1629. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  1630. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  1631. /* Initialize Correctable Error Mask Register */
  1632. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  1633. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  1634. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  1635. /* Initialize Advanced Error Capabilities and Control Register */
  1636. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  1637. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  1638. /* Don't enable ECRC generation or checking if unsupported */
  1639. if (!(reg32 & PCI_ERR_CAP_ECRC_GENC))
  1640. reg32 &= ~PCI_ERR_CAP_ECRC_GENE;
  1641. if (!(reg32 & PCI_ERR_CAP_ECRC_CHKC))
  1642. reg32 &= ~PCI_ERR_CAP_ECRC_CHKE;
  1643. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  1644. /*
  1645. * FIXME: The following two registers are not supported yet.
  1646. *
  1647. * o Secondary Uncorrectable Error Severity Register
  1648. * o Secondary Uncorrectable Error Mask Register
  1649. */
  1650. }
  1651. int pci_configure_extended_tags(struct pci_dev *dev, void *ign)
  1652. {
  1653. struct pci_host_bridge *host;
  1654. u32 cap;
  1655. u16 ctl;
  1656. int ret;
  1657. if (!pci_is_pcie(dev))
  1658. return 0;
  1659. ret = pcie_capability_read_dword(dev, PCI_EXP_DEVCAP, &cap);
  1660. if (ret)
  1661. return 0;
  1662. if (!(cap & PCI_EXP_DEVCAP_EXT_TAG))
  1663. return 0;
  1664. ret = pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  1665. if (ret)
  1666. return 0;
  1667. host = pci_find_host_bridge(dev->bus);
  1668. if (!host)
  1669. return 0;
  1670. /*
  1671. * If some device in the hierarchy doesn't handle Extended Tags
  1672. * correctly, make sure they're disabled.
  1673. */
  1674. if (host->no_ext_tags) {
  1675. if (ctl & PCI_EXP_DEVCTL_EXT_TAG) {
  1676. pci_info(dev, "disabling Extended Tags\n");
  1677. pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
  1678. PCI_EXP_DEVCTL_EXT_TAG);
  1679. }
  1680. return 0;
  1681. }
  1682. if (!(ctl & PCI_EXP_DEVCTL_EXT_TAG)) {
  1683. pci_info(dev, "enabling Extended Tags\n");
  1684. pcie_capability_set_word(dev, PCI_EXP_DEVCTL,
  1685. PCI_EXP_DEVCTL_EXT_TAG);
  1686. }
  1687. return 0;
  1688. }
  1689. /**
  1690. * pcie_relaxed_ordering_enabled - Probe for PCIe relaxed ordering enable
  1691. * @dev: PCI device to query
  1692. *
  1693. * Returns true if the device has enabled relaxed ordering attribute.
  1694. */
  1695. bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
  1696. {
  1697. u16 v;
  1698. pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &v);
  1699. return !!(v & PCI_EXP_DEVCTL_RELAX_EN);
  1700. }
  1701. EXPORT_SYMBOL(pcie_relaxed_ordering_enabled);
  1702. static void pci_configure_relaxed_ordering(struct pci_dev *dev)
  1703. {
  1704. struct pci_dev *root;
  1705. /* PCI_EXP_DEVICE_RELAX_EN is RsvdP in VFs */
  1706. if (dev->is_virtfn)
  1707. return;
  1708. if (!pcie_relaxed_ordering_enabled(dev))
  1709. return;
  1710. /*
  1711. * For now, we only deal with Relaxed Ordering issues with Root
  1712. * Ports. Peer-to-Peer DMA is another can of worms.
  1713. */
  1714. root = pci_find_pcie_root_port(dev);
  1715. if (!root)
  1716. return;
  1717. if (root->dev_flags & PCI_DEV_FLAGS_NO_RELAXED_ORDERING) {
  1718. pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
  1719. PCI_EXP_DEVCTL_RELAX_EN);
  1720. pci_info(dev, "Relaxed Ordering disabled because the Root Port didn't support it\n");
  1721. }
  1722. }
  1723. static void pci_configure_ltr(struct pci_dev *dev)
  1724. {
  1725. #ifdef CONFIG_PCIEASPM
  1726. struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
  1727. u32 cap;
  1728. struct pci_dev *bridge;
  1729. if (!host->native_ltr)
  1730. return;
  1731. if (!pci_is_pcie(dev))
  1732. return;
  1733. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
  1734. if (!(cap & PCI_EXP_DEVCAP2_LTR))
  1735. return;
  1736. /*
  1737. * Software must not enable LTR in an Endpoint unless the Root
  1738. * Complex and all intermediate Switches indicate support for LTR.
  1739. * PCIe r3.1, sec 6.18.
  1740. */
  1741. if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
  1742. dev->ltr_path = 1;
  1743. else {
  1744. bridge = pci_upstream_bridge(dev);
  1745. if (bridge && bridge->ltr_path)
  1746. dev->ltr_path = 1;
  1747. }
  1748. if (dev->ltr_path)
  1749. pcie_capability_set_word(dev, PCI_EXP_DEVCTL2,
  1750. PCI_EXP_DEVCTL2_LTR_EN);
  1751. #endif
  1752. }
  1753. static void pci_configure_eetlp_prefix(struct pci_dev *dev)
  1754. {
  1755. #ifdef CONFIG_PCI_PASID
  1756. struct pci_dev *bridge;
  1757. int pcie_type;
  1758. u32 cap;
  1759. if (!pci_is_pcie(dev))
  1760. return;
  1761. pcie_capability_read_dword(dev, PCI_EXP_DEVCAP2, &cap);
  1762. if (!(cap & PCI_EXP_DEVCAP2_EE_PREFIX))
  1763. return;
  1764. pcie_type = pci_pcie_type(dev);
  1765. if (pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  1766. pcie_type == PCI_EXP_TYPE_RC_END)
  1767. dev->eetlp_prefix_path = 1;
  1768. else {
  1769. bridge = pci_upstream_bridge(dev);
  1770. if (bridge && bridge->eetlp_prefix_path)
  1771. dev->eetlp_prefix_path = 1;
  1772. }
  1773. #endif
  1774. }
  1775. static void pci_configure_device(struct pci_dev *dev)
  1776. {
  1777. struct hotplug_params hpp;
  1778. int ret;
  1779. pci_configure_mps(dev);
  1780. pci_configure_extended_tags(dev, NULL);
  1781. pci_configure_relaxed_ordering(dev);
  1782. pci_configure_ltr(dev);
  1783. pci_configure_eetlp_prefix(dev);
  1784. memset(&hpp, 0, sizeof(hpp));
  1785. ret = pci_get_hp_params(dev, &hpp);
  1786. if (ret)
  1787. return;
  1788. program_hpp_type2(dev, hpp.t2);
  1789. program_hpp_type1(dev, hpp.t1);
  1790. program_hpp_type0(dev, hpp.t0);
  1791. }
  1792. static void pci_release_capabilities(struct pci_dev *dev)
  1793. {
  1794. pci_aer_exit(dev);
  1795. pci_vpd_release(dev);
  1796. pci_iov_release(dev);
  1797. pci_free_cap_save_buffers(dev);
  1798. }
  1799. /**
  1800. * pci_release_dev - Free a PCI device structure when all users of it are
  1801. * finished
  1802. * @dev: device that's been disconnected
  1803. *
  1804. * Will be called only by the device core when all users of this PCI device are
  1805. * done.
  1806. */
  1807. static void pci_release_dev(struct device *dev)
  1808. {
  1809. struct pci_dev *pci_dev;
  1810. pci_dev = to_pci_dev(dev);
  1811. pci_release_capabilities(pci_dev);
  1812. pci_release_of_node(pci_dev);
  1813. pcibios_release_device(pci_dev);
  1814. pci_bus_put(pci_dev->bus);
  1815. kfree(pci_dev->driver_override);
  1816. bitmap_free(pci_dev->dma_alias_mask);
  1817. kfree(pci_dev);
  1818. }
  1819. struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
  1820. {
  1821. struct pci_dev *dev;
  1822. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1823. if (!dev)
  1824. return NULL;
  1825. INIT_LIST_HEAD(&dev->bus_list);
  1826. dev->dev.type = &pci_dev_type;
  1827. dev->bus = pci_bus_get(bus);
  1828. return dev;
  1829. }
  1830. EXPORT_SYMBOL(pci_alloc_dev);
  1831. static bool pci_bus_crs_vendor_id(u32 l)
  1832. {
  1833. return (l & 0xffff) == 0x0001;
  1834. }
  1835. static bool pci_bus_wait_crs(struct pci_bus *bus, int devfn, u32 *l,
  1836. int timeout)
  1837. {
  1838. int delay = 1;
  1839. if (!pci_bus_crs_vendor_id(*l))
  1840. return true; /* not a CRS completion */
  1841. if (!timeout)
  1842. return false; /* CRS, but caller doesn't want to wait */
  1843. /*
  1844. * We got the reserved Vendor ID that indicates a completion with
  1845. * Configuration Request Retry Status (CRS). Retry until we get a
  1846. * valid Vendor ID or we time out.
  1847. */
  1848. while (pci_bus_crs_vendor_id(*l)) {
  1849. if (delay > timeout) {
  1850. pr_warn("pci %04x:%02x:%02x.%d: not ready after %dms; giving up\n",
  1851. pci_domain_nr(bus), bus->number,
  1852. PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
  1853. return false;
  1854. }
  1855. if (delay >= 1000)
  1856. pr_info("pci %04x:%02x:%02x.%d: not ready after %dms; waiting\n",
  1857. pci_domain_nr(bus), bus->number,
  1858. PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
  1859. msleep(delay);
  1860. delay *= 2;
  1861. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1862. return false;
  1863. }
  1864. if (delay >= 1000)
  1865. pr_info("pci %04x:%02x:%02x.%d: ready after %dms\n",
  1866. pci_domain_nr(bus), bus->number,
  1867. PCI_SLOT(devfn), PCI_FUNC(devfn), delay - 1);
  1868. return true;
  1869. }
  1870. bool pci_bus_generic_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
  1871. int timeout)
  1872. {
  1873. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1874. return false;
  1875. /* Some broken boards return 0 or ~0 if a slot is empty: */
  1876. if (*l == 0xffffffff || *l == 0x00000000 ||
  1877. *l == 0x0000ffff || *l == 0xffff0000)
  1878. return false;
  1879. if (pci_bus_crs_vendor_id(*l))
  1880. return pci_bus_wait_crs(bus, devfn, l, timeout);
  1881. return true;
  1882. }
  1883. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
  1884. int timeout)
  1885. {
  1886. #ifdef CONFIG_PCI_QUIRKS
  1887. struct pci_dev *bridge = bus->self;
  1888. /*
  1889. * Certain IDT switches have an issue where they improperly trigger
  1890. * ACS Source Validation errors on completions for config reads.
  1891. */
  1892. if (bridge && bridge->vendor == PCI_VENDOR_ID_IDT &&
  1893. bridge->device == 0x80b5)
  1894. return pci_idt_bus_quirk(bus, devfn, l, timeout);
  1895. #endif
  1896. return pci_bus_generic_read_dev_vendor_id(bus, devfn, l, timeout);
  1897. }
  1898. EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
  1899. /*
  1900. * Read the config data for a PCI device, sanity-check it,
  1901. * and fill in the dev structure.
  1902. */
  1903. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  1904. {
  1905. struct pci_dev *dev;
  1906. u32 l;
  1907. if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
  1908. return NULL;
  1909. dev = pci_alloc_dev(bus);
  1910. if (!dev)
  1911. return NULL;
  1912. dev->devfn = devfn;
  1913. dev->vendor = l & 0xffff;
  1914. dev->device = (l >> 16) & 0xffff;
  1915. pci_set_of_node(dev);
  1916. if (pci_setup_device(dev)) {
  1917. pci_bus_put(dev->bus);
  1918. kfree(dev);
  1919. return NULL;
  1920. }
  1921. return dev;
  1922. }
  1923. static void pcie_report_downtraining(struct pci_dev *dev)
  1924. {
  1925. if (!pci_is_pcie(dev))
  1926. return;
  1927. /* Look from the device up to avoid downstream ports with no devices */
  1928. if ((pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT) &&
  1929. (pci_pcie_type(dev) != PCI_EXP_TYPE_LEG_END) &&
  1930. (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM))
  1931. return;
  1932. /* Multi-function PCIe devices share the same link/status */
  1933. if (PCI_FUNC(dev->devfn) != 0 || dev->is_virtfn)
  1934. return;
  1935. /* Print link status only if the device is constrained by the fabric */
  1936. __pcie_print_link_status(dev, false);
  1937. }
  1938. static void pci_init_capabilities(struct pci_dev *dev)
  1939. {
  1940. /* Enhanced Allocation */
  1941. pci_ea_init(dev);
  1942. /* Setup MSI caps & disable MSI/MSI-X interrupts */
  1943. pci_msi_setup_pci_dev(dev);
  1944. /* Buffers for saving PCIe and PCI-X capabilities */
  1945. pci_allocate_cap_save_buffers(dev);
  1946. /* Power Management */
  1947. pci_pm_init(dev);
  1948. /* Vital Product Data */
  1949. pci_vpd_init(dev);
  1950. /* Alternative Routing-ID Forwarding */
  1951. pci_configure_ari(dev);
  1952. /* Single Root I/O Virtualization */
  1953. pci_iov_init(dev);
  1954. /* Address Translation Services */
  1955. pci_ats_init(dev);
  1956. /* Enable ACS P2P upstream forwarding */
  1957. pci_enable_acs(dev);
  1958. /* Precision Time Measurement */
  1959. pci_ptm_init(dev);
  1960. /* Advanced Error Reporting */
  1961. pci_aer_init(dev);
  1962. pcie_report_downtraining(dev);
  1963. if (pci_probe_reset_function(dev) == 0)
  1964. dev->reset_fn = 1;
  1965. }
  1966. /*
  1967. * This is the equivalent of pci_host_bridge_msi_domain() that acts on
  1968. * devices. Firmware interfaces that can select the MSI domain on a
  1969. * per-device basis should be called from here.
  1970. */
  1971. static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
  1972. {
  1973. struct irq_domain *d;
  1974. /*
  1975. * If a domain has been set through the pcibios_add_device()
  1976. * callback, then this is the one (platform code knows best).
  1977. */
  1978. d = dev_get_msi_domain(&dev->dev);
  1979. if (d)
  1980. return d;
  1981. /*
  1982. * Let's see if we have a firmware interface able to provide
  1983. * the domain.
  1984. */
  1985. d = pci_msi_get_device_domain(dev);
  1986. if (d)
  1987. return d;
  1988. return NULL;
  1989. }
  1990. static void pci_set_msi_domain(struct pci_dev *dev)
  1991. {
  1992. struct irq_domain *d;
  1993. /*
  1994. * If the platform or firmware interfaces cannot supply a
  1995. * device-specific MSI domain, then inherit the default domain
  1996. * from the host bridge itself.
  1997. */
  1998. d = pci_dev_msi_domain(dev);
  1999. if (!d)
  2000. d = dev_get_msi_domain(&dev->bus->dev);
  2001. dev_set_msi_domain(&dev->dev, d);
  2002. }
  2003. void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  2004. {
  2005. int ret;
  2006. pci_configure_device(dev);
  2007. device_initialize(&dev->dev);
  2008. dev->dev.release = pci_release_dev;
  2009. set_dev_node(&dev->dev, pcibus_to_node(bus));
  2010. dev->dev.dma_mask = &dev->dma_mask;
  2011. dev->dev.dma_parms = &dev->dma_parms;
  2012. dev->dev.coherent_dma_mask = 0xffffffffull;
  2013. dma_set_max_seg_size(&dev->dev, 65536);
  2014. dma_set_seg_boundary(&dev->dev, 0xffffffff);
  2015. /* Fix up broken headers */
  2016. pci_fixup_device(pci_fixup_header, dev);
  2017. /* Moved out from quirk header fixup code */
  2018. pci_reassigndev_resource_alignment(dev);
  2019. /* Clear the state_saved flag */
  2020. dev->state_saved = false;
  2021. /* Initialize various capabilities */
  2022. pci_init_capabilities(dev);
  2023. /*
  2024. * Add the device to our list of discovered devices
  2025. * and the bus list for fixup functions, etc.
  2026. */
  2027. down_write(&pci_bus_sem);
  2028. list_add_tail(&dev->bus_list, &bus->devices);
  2029. up_write(&pci_bus_sem);
  2030. ret = pcibios_add_device(dev);
  2031. WARN_ON(ret < 0);
  2032. /* Set up MSI IRQ domain */
  2033. pci_set_msi_domain(dev);
  2034. /* Notifier could use PCI capabilities */
  2035. dev->match_driver = false;
  2036. ret = device_add(&dev->dev);
  2037. WARN_ON(ret < 0);
  2038. }
  2039. struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
  2040. {
  2041. struct pci_dev *dev;
  2042. dev = pci_get_slot(bus, devfn);
  2043. if (dev) {
  2044. pci_dev_put(dev);
  2045. return dev;
  2046. }
  2047. dev = pci_scan_device(bus, devfn);
  2048. if (!dev)
  2049. return NULL;
  2050. pci_device_add(dev, bus);
  2051. return dev;
  2052. }
  2053. EXPORT_SYMBOL(pci_scan_single_device);
  2054. static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
  2055. {
  2056. int pos;
  2057. u16 cap = 0;
  2058. unsigned next_fn;
  2059. if (pci_ari_enabled(bus)) {
  2060. if (!dev)
  2061. return 0;
  2062. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  2063. if (!pos)
  2064. return 0;
  2065. pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
  2066. next_fn = PCI_ARI_CAP_NFN(cap);
  2067. if (next_fn <= fn)
  2068. return 0; /* protect against malformed list */
  2069. return next_fn;
  2070. }
  2071. /* dev may be NULL for non-contiguous multifunction devices */
  2072. if (!dev || dev->multifunction)
  2073. return (fn + 1) % 8;
  2074. return 0;
  2075. }
  2076. static int only_one_child(struct pci_bus *bus)
  2077. {
  2078. struct pci_dev *bridge = bus->self;
  2079. /*
  2080. * Systems with unusual topologies set PCI_SCAN_ALL_PCIE_DEVS so
  2081. * we scan for all possible devices, not just Device 0.
  2082. */
  2083. if (pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
  2084. return 0;
  2085. /*
  2086. * A PCIe Downstream Port normally leads to a Link with only Device
  2087. * 0 on it (PCIe spec r3.1, sec 7.3.1). As an optimization, scan
  2088. * only for Device 0 in that situation.
  2089. *
  2090. * Checking has_secondary_link is a hack to identify Downstream
  2091. * Ports because sometimes Switches are configured such that the
  2092. * PCIe Port Type labels are backwards.
  2093. */
  2094. if (bridge && pci_is_pcie(bridge) && bridge->has_secondary_link)
  2095. return 1;
  2096. return 0;
  2097. }
  2098. /**
  2099. * pci_scan_slot - Scan a PCI slot on a bus for devices
  2100. * @bus: PCI bus to scan
  2101. * @devfn: slot number to scan (must have zero function)
  2102. *
  2103. * Scan a PCI slot on the specified PCI bus for devices, adding
  2104. * discovered devices to the @bus->devices list. New devices
  2105. * will not have is_added set.
  2106. *
  2107. * Returns the number of new devices found.
  2108. */
  2109. int pci_scan_slot(struct pci_bus *bus, int devfn)
  2110. {
  2111. unsigned fn, nr = 0;
  2112. struct pci_dev *dev;
  2113. if (only_one_child(bus) && (devfn > 0))
  2114. return 0; /* Already scanned the entire slot */
  2115. dev = pci_scan_single_device(bus, devfn);
  2116. if (!dev)
  2117. return 0;
  2118. if (!pci_dev_is_added(dev))
  2119. nr++;
  2120. for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
  2121. dev = pci_scan_single_device(bus, devfn + fn);
  2122. if (dev) {
  2123. if (!pci_dev_is_added(dev))
  2124. nr++;
  2125. dev->multifunction = 1;
  2126. }
  2127. }
  2128. /* Only one slot has PCIe device */
  2129. if (bus->self && nr)
  2130. pcie_aspm_init_link_state(bus->self);
  2131. return nr;
  2132. }
  2133. EXPORT_SYMBOL(pci_scan_slot);
  2134. static int pcie_find_smpss(struct pci_dev *dev, void *data)
  2135. {
  2136. u8 *smpss = data;
  2137. if (!pci_is_pcie(dev))
  2138. return 0;
  2139. /*
  2140. * We don't have a way to change MPS settings on devices that have
  2141. * drivers attached. A hot-added device might support only the minimum
  2142. * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
  2143. * where devices may be hot-added, we limit the fabric MPS to 128 so
  2144. * hot-added devices will work correctly.
  2145. *
  2146. * However, if we hot-add a device to a slot directly below a Root
  2147. * Port, it's impossible for there to be other existing devices below
  2148. * the port. We don't limit the MPS in this case because we can
  2149. * reconfigure MPS on both the Root Port and the hot-added device,
  2150. * and there are no other devices involved.
  2151. *
  2152. * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
  2153. */
  2154. if (dev->is_hotplug_bridge &&
  2155. pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
  2156. *smpss = 0;
  2157. if (*smpss > dev->pcie_mpss)
  2158. *smpss = dev->pcie_mpss;
  2159. return 0;
  2160. }
  2161. static void pcie_write_mps(struct pci_dev *dev, int mps)
  2162. {
  2163. int rc;
  2164. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  2165. mps = 128 << dev->pcie_mpss;
  2166. if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
  2167. dev->bus->self)
  2168. /*
  2169. * For "Performance", the assumption is made that
  2170. * downstream communication will never be larger than
  2171. * the MRRS. So, the MPS only needs to be configured
  2172. * for the upstream communication. This being the case,
  2173. * walk from the top down and set the MPS of the child
  2174. * to that of the parent bus.
  2175. *
  2176. * Configure the device MPS with the smaller of the
  2177. * device MPSS or the bridge MPS (which is assumed to be
  2178. * properly configured at this point to the largest
  2179. * allowable MPS based on its parent bus).
  2180. */
  2181. mps = min(mps, pcie_get_mps(dev->bus->self));
  2182. }
  2183. rc = pcie_set_mps(dev, mps);
  2184. if (rc)
  2185. pci_err(dev, "Failed attempting to set the MPS\n");
  2186. }
  2187. static void pcie_write_mrrs(struct pci_dev *dev)
  2188. {
  2189. int rc, mrrs;
  2190. /*
  2191. * In the "safe" case, do not configure the MRRS. There appear to be
  2192. * issues with setting MRRS to 0 on a number of devices.
  2193. */
  2194. if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
  2195. return;
  2196. /*
  2197. * For max performance, the MRRS must be set to the largest supported
  2198. * value. However, it cannot be configured larger than the MPS the
  2199. * device or the bus can support. This should already be properly
  2200. * configured by a prior call to pcie_write_mps().
  2201. */
  2202. mrrs = pcie_get_mps(dev);
  2203. /*
  2204. * MRRS is a R/W register. Invalid values can be written, but a
  2205. * subsequent read will verify if the value is acceptable or not.
  2206. * If the MRRS value provided is not acceptable (e.g., too large),
  2207. * shrink the value until it is acceptable to the HW.
  2208. */
  2209. while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
  2210. rc = pcie_set_readrq(dev, mrrs);
  2211. if (!rc)
  2212. break;
  2213. pci_warn(dev, "Failed attempting to set the MRRS\n");
  2214. mrrs /= 2;
  2215. }
  2216. if (mrrs < 128)
  2217. pci_err(dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n");
  2218. }
  2219. static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
  2220. {
  2221. int mps, orig_mps;
  2222. if (!pci_is_pcie(dev))
  2223. return 0;
  2224. if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
  2225. pcie_bus_config == PCIE_BUS_DEFAULT)
  2226. return 0;
  2227. mps = 128 << *(u8 *)data;
  2228. orig_mps = pcie_get_mps(dev);
  2229. pcie_write_mps(dev, mps);
  2230. pcie_write_mrrs(dev);
  2231. pci_info(dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
  2232. pcie_get_mps(dev), 128 << dev->pcie_mpss,
  2233. orig_mps, pcie_get_readrq(dev));
  2234. return 0;
  2235. }
  2236. /*
  2237. * pcie_bus_configure_settings() requires that pci_walk_bus work in a top-down,
  2238. * parents then children fashion. If this changes, then this code will not
  2239. * work as designed.
  2240. */
  2241. void pcie_bus_configure_settings(struct pci_bus *bus)
  2242. {
  2243. u8 smpss = 0;
  2244. if (!bus->self)
  2245. return;
  2246. if (!pci_is_pcie(bus->self))
  2247. return;
  2248. /*
  2249. * FIXME - Peer to peer DMA is possible, though the endpoint would need
  2250. * to be aware of the MPS of the destination. To work around this,
  2251. * simply force the MPS of the entire system to the smallest possible.
  2252. */
  2253. if (pcie_bus_config == PCIE_BUS_PEER2PEER)
  2254. smpss = 0;
  2255. if (pcie_bus_config == PCIE_BUS_SAFE) {
  2256. smpss = bus->self->pcie_mpss;
  2257. pcie_find_smpss(bus->self, &smpss);
  2258. pci_walk_bus(bus, pcie_find_smpss, &smpss);
  2259. }
  2260. pcie_bus_configure_set(bus->self, &smpss);
  2261. pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
  2262. }
  2263. EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
  2264. /*
  2265. * Called after each bus is probed, but before its children are examined. This
  2266. * is marked as __weak because multiple architectures define it.
  2267. */
  2268. void __weak pcibios_fixup_bus(struct pci_bus *bus)
  2269. {
  2270. /* nothing to do, expected to be removed in the future */
  2271. }
  2272. /**
  2273. * pci_scan_child_bus_extend() - Scan devices below a bus
  2274. * @bus: Bus to scan for devices
  2275. * @available_buses: Total number of buses available (%0 does not try to
  2276. * extend beyond the minimal)
  2277. *
  2278. * Scans devices below @bus including subordinate buses. Returns new
  2279. * subordinate number including all the found devices. Passing
  2280. * @available_buses causes the remaining bus space to be distributed
  2281. * equally between hotplug-capable bridges to allow future extension of the
  2282. * hierarchy.
  2283. */
  2284. static unsigned int pci_scan_child_bus_extend(struct pci_bus *bus,
  2285. unsigned int available_buses)
  2286. {
  2287. unsigned int used_buses, normal_bridges = 0, hotplug_bridges = 0;
  2288. unsigned int start = bus->busn_res.start;
  2289. unsigned int devfn, fn, cmax, max = start;
  2290. struct pci_dev *dev;
  2291. int nr_devs;
  2292. dev_dbg(&bus->dev, "scanning bus\n");
  2293. /* Go find them, Rover! */
  2294. for (devfn = 0; devfn < 256; devfn += 8) {
  2295. nr_devs = pci_scan_slot(bus, devfn);
  2296. /*
  2297. * The Jailhouse hypervisor may pass individual functions of a
  2298. * multi-function device to a guest without passing function 0.
  2299. * Look for them as well.
  2300. */
  2301. if (jailhouse_paravirt() && nr_devs == 0) {
  2302. for (fn = 1; fn < 8; fn++) {
  2303. dev = pci_scan_single_device(bus, devfn + fn);
  2304. if (dev)
  2305. dev->multifunction = 1;
  2306. }
  2307. }
  2308. }
  2309. /* Reserve buses for SR-IOV capability */
  2310. used_buses = pci_iov_bus_range(bus);
  2311. max += used_buses;
  2312. /*
  2313. * After performing arch-dependent fixup of the bus, look behind
  2314. * all PCI-to-PCI bridges on this bus.
  2315. */
  2316. if (!bus->is_added) {
  2317. dev_dbg(&bus->dev, "fixups for bus\n");
  2318. pcibios_fixup_bus(bus);
  2319. bus->is_added = 1;
  2320. }
  2321. /*
  2322. * Calculate how many hotplug bridges and normal bridges there
  2323. * are on this bus. We will distribute the additional available
  2324. * buses between hotplug bridges.
  2325. */
  2326. for_each_pci_bridge(dev, bus) {
  2327. if (dev->is_hotplug_bridge)
  2328. hotplug_bridges++;
  2329. else
  2330. normal_bridges++;
  2331. }
  2332. /*
  2333. * Scan bridges that are already configured. We don't touch them
  2334. * unless they are misconfigured (which will be done in the second
  2335. * scan below).
  2336. */
  2337. for_each_pci_bridge(dev, bus) {
  2338. cmax = max;
  2339. max = pci_scan_bridge_extend(bus, dev, max, 0, 0);
  2340. /*
  2341. * Reserve one bus for each bridge now to avoid extending
  2342. * hotplug bridges too much during the second scan below.
  2343. */
  2344. used_buses++;
  2345. if (cmax - max > 1)
  2346. used_buses += cmax - max - 1;
  2347. }
  2348. /* Scan bridges that need to be reconfigured */
  2349. for_each_pci_bridge(dev, bus) {
  2350. unsigned int buses = 0;
  2351. if (!hotplug_bridges && normal_bridges == 1) {
  2352. /*
  2353. * There is only one bridge on the bus (upstream
  2354. * port) so it gets all available buses which it
  2355. * can then distribute to the possible hotplug
  2356. * bridges below.
  2357. */
  2358. buses = available_buses;
  2359. } else if (dev->is_hotplug_bridge) {
  2360. /*
  2361. * Distribute the extra buses between hotplug
  2362. * bridges if any.
  2363. */
  2364. buses = available_buses / hotplug_bridges;
  2365. buses = min(buses, available_buses - used_buses + 1);
  2366. }
  2367. cmax = max;
  2368. max = pci_scan_bridge_extend(bus, dev, cmax, buses, 1);
  2369. /* One bus is already accounted so don't add it again */
  2370. if (max - cmax > 1)
  2371. used_buses += max - cmax - 1;
  2372. }
  2373. /*
  2374. * Make sure a hotplug bridge has at least the minimum requested
  2375. * number of buses but allow it to grow up to the maximum available
  2376. * bus number of there is room.
  2377. */
  2378. if (bus->self && bus->self->is_hotplug_bridge) {
  2379. used_buses = max_t(unsigned int, available_buses,
  2380. pci_hotplug_bus_size - 1);
  2381. if (max - start < used_buses) {
  2382. max = start + used_buses;
  2383. /* Do not allocate more buses than we have room left */
  2384. if (max > bus->busn_res.end)
  2385. max = bus->busn_res.end;
  2386. dev_dbg(&bus->dev, "%pR extended by %#02x\n",
  2387. &bus->busn_res, max - start);
  2388. }
  2389. }
  2390. /*
  2391. * We've scanned the bus and so we know all about what's on
  2392. * the other side of any bridges that may be on this bus plus
  2393. * any devices.
  2394. *
  2395. * Return how far we've got finding sub-buses.
  2396. */
  2397. dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
  2398. return max;
  2399. }
  2400. /**
  2401. * pci_scan_child_bus() - Scan devices below a bus
  2402. * @bus: Bus to scan for devices
  2403. *
  2404. * Scans devices below @bus including subordinate buses. Returns new
  2405. * subordinate number including all the found devices.
  2406. */
  2407. unsigned int pci_scan_child_bus(struct pci_bus *bus)
  2408. {
  2409. return pci_scan_child_bus_extend(bus, 0);
  2410. }
  2411. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  2412. /**
  2413. * pcibios_root_bridge_prepare - Platform-specific host bridge setup
  2414. * @bridge: Host bridge to set up
  2415. *
  2416. * Default empty implementation. Replace with an architecture-specific setup
  2417. * routine, if necessary.
  2418. */
  2419. int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  2420. {
  2421. return 0;
  2422. }
  2423. void __weak pcibios_add_bus(struct pci_bus *bus)
  2424. {
  2425. }
  2426. void __weak pcibios_remove_bus(struct pci_bus *bus)
  2427. {
  2428. }
  2429. struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
  2430. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  2431. {
  2432. int error;
  2433. struct pci_host_bridge *bridge;
  2434. bridge = pci_alloc_host_bridge(0);
  2435. if (!bridge)
  2436. return NULL;
  2437. bridge->dev.parent = parent;
  2438. list_splice_init(resources, &bridge->windows);
  2439. bridge->sysdata = sysdata;
  2440. bridge->busnr = bus;
  2441. bridge->ops = ops;
  2442. error = pci_register_host_bridge(bridge);
  2443. if (error < 0)
  2444. goto err_out;
  2445. return bridge->bus;
  2446. err_out:
  2447. kfree(bridge);
  2448. return NULL;
  2449. }
  2450. EXPORT_SYMBOL_GPL(pci_create_root_bus);
  2451. int pci_host_probe(struct pci_host_bridge *bridge)
  2452. {
  2453. struct pci_bus *bus, *child;
  2454. int ret;
  2455. ret = pci_scan_root_bus_bridge(bridge);
  2456. if (ret < 0) {
  2457. dev_err(bridge->dev.parent, "Scanning root bridge failed");
  2458. return ret;
  2459. }
  2460. bus = bridge->bus;
  2461. /*
  2462. * We insert PCI resources into the iomem_resource and
  2463. * ioport_resource trees in either pci_bus_claim_resources()
  2464. * or pci_bus_assign_resources().
  2465. */
  2466. if (pci_has_flag(PCI_PROBE_ONLY)) {
  2467. pci_bus_claim_resources(bus);
  2468. } else {
  2469. pci_bus_size_bridges(bus);
  2470. pci_bus_assign_resources(bus);
  2471. list_for_each_entry(child, &bus->children, node)
  2472. pcie_bus_configure_settings(child);
  2473. }
  2474. pci_bus_add_devices(bus);
  2475. return 0;
  2476. }
  2477. EXPORT_SYMBOL_GPL(pci_host_probe);
  2478. int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
  2479. {
  2480. struct resource *res = &b->busn_res;
  2481. struct resource *parent_res, *conflict;
  2482. res->start = bus;
  2483. res->end = bus_max;
  2484. res->flags = IORESOURCE_BUS;
  2485. if (!pci_is_root_bus(b))
  2486. parent_res = &b->parent->busn_res;
  2487. else {
  2488. parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
  2489. res->flags |= IORESOURCE_PCI_FIXED;
  2490. }
  2491. conflict = request_resource_conflict(parent_res, res);
  2492. if (conflict)
  2493. dev_printk(KERN_DEBUG, &b->dev,
  2494. "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
  2495. res, pci_is_root_bus(b) ? "domain " : "",
  2496. parent_res, conflict->name, conflict);
  2497. return conflict == NULL;
  2498. }
  2499. int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
  2500. {
  2501. struct resource *res = &b->busn_res;
  2502. struct resource old_res = *res;
  2503. resource_size_t size;
  2504. int ret;
  2505. if (res->start > bus_max)
  2506. return -EINVAL;
  2507. size = bus_max - res->start + 1;
  2508. ret = adjust_resource(res, res->start, size);
  2509. dev_printk(KERN_DEBUG, &b->dev,
  2510. "busn_res: %pR end %s updated to %02x\n",
  2511. &old_res, ret ? "can not be" : "is", bus_max);
  2512. if (!ret && !res->parent)
  2513. pci_bus_insert_busn_res(b, res->start, res->end);
  2514. return ret;
  2515. }
  2516. void pci_bus_release_busn_res(struct pci_bus *b)
  2517. {
  2518. struct resource *res = &b->busn_res;
  2519. int ret;
  2520. if (!res->flags || !res->parent)
  2521. return;
  2522. ret = release_resource(res);
  2523. dev_printk(KERN_DEBUG, &b->dev,
  2524. "busn_res: %pR %s released\n",
  2525. res, ret ? "can not be" : "is");
  2526. }
  2527. int pci_scan_root_bus_bridge(struct pci_host_bridge *bridge)
  2528. {
  2529. struct resource_entry *window;
  2530. bool found = false;
  2531. struct pci_bus *b;
  2532. int max, bus, ret;
  2533. if (!bridge)
  2534. return -EINVAL;
  2535. resource_list_for_each_entry(window, &bridge->windows)
  2536. if (window->res->flags & IORESOURCE_BUS) {
  2537. found = true;
  2538. break;
  2539. }
  2540. ret = pci_register_host_bridge(bridge);
  2541. if (ret < 0)
  2542. return ret;
  2543. b = bridge->bus;
  2544. bus = bridge->busnr;
  2545. if (!found) {
  2546. dev_info(&b->dev,
  2547. "No busn resource found for root bus, will use [bus %02x-ff]\n",
  2548. bus);
  2549. pci_bus_insert_busn_res(b, bus, 255);
  2550. }
  2551. max = pci_scan_child_bus(b);
  2552. if (!found)
  2553. pci_bus_update_busn_res_end(b, max);
  2554. return 0;
  2555. }
  2556. EXPORT_SYMBOL(pci_scan_root_bus_bridge);
  2557. struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
  2558. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  2559. {
  2560. struct resource_entry *window;
  2561. bool found = false;
  2562. struct pci_bus *b;
  2563. int max;
  2564. resource_list_for_each_entry(window, resources)
  2565. if (window->res->flags & IORESOURCE_BUS) {
  2566. found = true;
  2567. break;
  2568. }
  2569. b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
  2570. if (!b)
  2571. return NULL;
  2572. if (!found) {
  2573. dev_info(&b->dev,
  2574. "No busn resource found for root bus, will use [bus %02x-ff]\n",
  2575. bus);
  2576. pci_bus_insert_busn_res(b, bus, 255);
  2577. }
  2578. max = pci_scan_child_bus(b);
  2579. if (!found)
  2580. pci_bus_update_busn_res_end(b, max);
  2581. return b;
  2582. }
  2583. EXPORT_SYMBOL(pci_scan_root_bus);
  2584. struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
  2585. void *sysdata)
  2586. {
  2587. LIST_HEAD(resources);
  2588. struct pci_bus *b;
  2589. pci_add_resource(&resources, &ioport_resource);
  2590. pci_add_resource(&resources, &iomem_resource);
  2591. pci_add_resource(&resources, &busn_resource);
  2592. b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
  2593. if (b) {
  2594. pci_scan_child_bus(b);
  2595. } else {
  2596. pci_free_resource_list(&resources);
  2597. }
  2598. return b;
  2599. }
  2600. EXPORT_SYMBOL(pci_scan_bus);
  2601. /**
  2602. * pci_rescan_bus_bridge_resize - Scan a PCI bus for devices
  2603. * @bridge: PCI bridge for the bus to scan
  2604. *
  2605. * Scan a PCI bus and child buses for new devices, add them,
  2606. * and enable them, resizing bridge mmio/io resource if necessary
  2607. * and possible. The caller must ensure the child devices are already
  2608. * removed for resizing to occur.
  2609. *
  2610. * Returns the max number of subordinate bus discovered.
  2611. */
  2612. unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
  2613. {
  2614. unsigned int max;
  2615. struct pci_bus *bus = bridge->subordinate;
  2616. max = pci_scan_child_bus(bus);
  2617. pci_assign_unassigned_bridge_resources(bridge);
  2618. pci_bus_add_devices(bus);
  2619. return max;
  2620. }
  2621. /**
  2622. * pci_rescan_bus - Scan a PCI bus for devices
  2623. * @bus: PCI bus to scan
  2624. *
  2625. * Scan a PCI bus and child buses for new devices, add them,
  2626. * and enable them.
  2627. *
  2628. * Returns the max number of subordinate bus discovered.
  2629. */
  2630. unsigned int pci_rescan_bus(struct pci_bus *bus)
  2631. {
  2632. unsigned int max;
  2633. max = pci_scan_child_bus(bus);
  2634. pci_assign_unassigned_bus_resources(bus);
  2635. pci_bus_add_devices(bus);
  2636. return max;
  2637. }
  2638. EXPORT_SYMBOL_GPL(pci_rescan_bus);
  2639. /*
  2640. * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
  2641. * routines should always be executed under this mutex.
  2642. */
  2643. static DEFINE_MUTEX(pci_rescan_remove_lock);
  2644. void pci_lock_rescan_remove(void)
  2645. {
  2646. mutex_lock(&pci_rescan_remove_lock);
  2647. }
  2648. EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
  2649. void pci_unlock_rescan_remove(void)
  2650. {
  2651. mutex_unlock(&pci_rescan_remove_lock);
  2652. }
  2653. EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
  2654. static int __init pci_sort_bf_cmp(const struct device *d_a,
  2655. const struct device *d_b)
  2656. {
  2657. const struct pci_dev *a = to_pci_dev(d_a);
  2658. const struct pci_dev *b = to_pci_dev(d_b);
  2659. if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
  2660. else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
  2661. if (a->bus->number < b->bus->number) return -1;
  2662. else if (a->bus->number > b->bus->number) return 1;
  2663. if (a->devfn < b->devfn) return -1;
  2664. else if (a->devfn > b->devfn) return 1;
  2665. return 0;
  2666. }
  2667. void __init pci_sort_breadthfirst(void)
  2668. {
  2669. bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
  2670. }
  2671. int pci_hp_add_bridge(struct pci_dev *dev)
  2672. {
  2673. struct pci_bus *parent = dev->bus;
  2674. int busnr, start = parent->busn_res.start;
  2675. unsigned int available_buses = 0;
  2676. int end = parent->busn_res.end;
  2677. for (busnr = start; busnr <= end; busnr++) {
  2678. if (!pci_find_bus(pci_domain_nr(parent), busnr))
  2679. break;
  2680. }
  2681. if (busnr-- > end) {
  2682. pci_err(dev, "No bus number available for hot-added bridge\n");
  2683. return -1;
  2684. }
  2685. /* Scan bridges that are already configured */
  2686. busnr = pci_scan_bridge(parent, dev, busnr, 0);
  2687. /*
  2688. * Distribute the available bus numbers between hotplug-capable
  2689. * bridges to make extending the chain later possible.
  2690. */
  2691. available_buses = end - busnr;
  2692. /* Scan bridges that need to be reconfigured */
  2693. pci_scan_bridge_extend(parent, dev, busnr, available_buses, 1);
  2694. if (!dev->subordinate)
  2695. return -1;
  2696. return 0;
  2697. }
  2698. EXPORT_SYMBOL_GPL(pci_hp_add_bridge);