probe.c 79 KB

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