probe.c 86 KB

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