probe.c 92 KB

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