probe.c 82 KB

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