probe.c 87 KB

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