probe.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  1. /*
  2. * probe.c - PCI detection and setup code
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/of_device.h>
  9. #include <linux/of_pci.h>
  10. #include <linux/pci_hotplug.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/pci-aspm.h>
  15. #include <linux/aer.h>
  16. #include <linux/acpi.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/pm_runtime.h>
  19. #include "pci.h"
  20. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  21. #define CARDBUS_RESERVE_BUSNR 3
  22. static struct resource busn_resource = {
  23. .name = "PCI busn",
  24. .start = 0,
  25. .end = 255,
  26. .flags = IORESOURCE_BUS,
  27. };
  28. /* Ugh. Need to stop exporting this to modules. */
  29. LIST_HEAD(pci_root_buses);
  30. EXPORT_SYMBOL(pci_root_buses);
  31. static LIST_HEAD(pci_domain_busn_res_list);
  32. struct pci_domain_busn_res {
  33. struct list_head list;
  34. struct resource res;
  35. int domain_nr;
  36. };
  37. static struct resource *get_pci_domain_busn_res(int domain_nr)
  38. {
  39. struct pci_domain_busn_res *r;
  40. list_for_each_entry(r, &pci_domain_busn_res_list, list)
  41. if (r->domain_nr == domain_nr)
  42. return &r->res;
  43. r = kzalloc(sizeof(*r), GFP_KERNEL);
  44. if (!r)
  45. return NULL;
  46. r->domain_nr = domain_nr;
  47. r->res.start = 0;
  48. r->res.end = 0xff;
  49. r->res.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED;
  50. list_add_tail(&r->list, &pci_domain_busn_res_list);
  51. return &r->res;
  52. }
  53. static int find_anything(struct device *dev, void *data)
  54. {
  55. return 1;
  56. }
  57. /*
  58. * Some device drivers need know if pci is initiated.
  59. * Basically, we think pci is not initiated when there
  60. * is no device to be found on the pci_bus_type.
  61. */
  62. int no_pci_devices(void)
  63. {
  64. struct device *dev;
  65. int no_devices;
  66. dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
  67. no_devices = (dev == NULL);
  68. put_device(dev);
  69. return no_devices;
  70. }
  71. EXPORT_SYMBOL(no_pci_devices);
  72. /*
  73. * PCI Bus Class
  74. */
  75. static void release_pcibus_dev(struct device *dev)
  76. {
  77. struct pci_bus *pci_bus = to_pci_bus(dev);
  78. put_device(pci_bus->bridge);
  79. pci_bus_remove_resources(pci_bus);
  80. pci_release_bus_of_node(pci_bus);
  81. kfree(pci_bus);
  82. }
  83. static struct class pcibus_class = {
  84. .name = "pci_bus",
  85. .dev_release = &release_pcibus_dev,
  86. .dev_groups = pcibus_groups,
  87. };
  88. static int __init pcibus_class_init(void)
  89. {
  90. return class_register(&pcibus_class);
  91. }
  92. postcore_initcall(pcibus_class_init);
  93. static u64 pci_size(u64 base, u64 maxbase, u64 mask)
  94. {
  95. u64 size = mask & maxbase; /* Find the significant bits */
  96. if (!size)
  97. return 0;
  98. /* Get the lowest of them to find the decode size, and
  99. from that the extent. */
  100. size = (size & ~(size-1)) - 1;
  101. /* base == maxbase can be valid only if the BAR has
  102. already been programmed with all 1s. */
  103. if (base == maxbase && ((base | size) & mask) != mask)
  104. return 0;
  105. return size;
  106. }
  107. static inline unsigned long decode_bar(struct pci_dev *dev, u32 bar)
  108. {
  109. u32 mem_type;
  110. unsigned long flags;
  111. if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  112. flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
  113. flags |= IORESOURCE_IO;
  114. return flags;
  115. }
  116. flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
  117. flags |= IORESOURCE_MEM;
  118. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  119. flags |= IORESOURCE_PREFETCH;
  120. mem_type = bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  121. switch (mem_type) {
  122. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  123. break;
  124. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  125. /* 1M mem BAR treated as 32-bit BAR */
  126. break;
  127. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  128. flags |= IORESOURCE_MEM_64;
  129. break;
  130. default:
  131. /* mem unknown type treated as 32-bit BAR */
  132. break;
  133. }
  134. return flags;
  135. }
  136. #define PCI_COMMAND_DECODE_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_IO)
  137. /**
  138. * pci_read_base - read a PCI BAR
  139. * @dev: the PCI device
  140. * @type: type of the BAR
  141. * @res: resource buffer to be filled in
  142. * @pos: BAR position in the config space
  143. *
  144. * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
  145. */
  146. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  147. struct resource *res, unsigned int pos)
  148. {
  149. u32 l, sz, mask;
  150. u64 l64, sz64, mask64;
  151. u16 orig_cmd;
  152. struct pci_bus_region region, inverted_region;
  153. mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
  154. /* No printks while decoding is disabled! */
  155. if (!dev->mmio_always_on) {
  156. pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
  157. if (orig_cmd & PCI_COMMAND_DECODE_ENABLE) {
  158. pci_write_config_word(dev, PCI_COMMAND,
  159. orig_cmd & ~PCI_COMMAND_DECODE_ENABLE);
  160. }
  161. }
  162. res->name = pci_name(dev);
  163. pci_read_config_dword(dev, pos, &l);
  164. pci_write_config_dword(dev, pos, l | mask);
  165. pci_read_config_dword(dev, pos, &sz);
  166. pci_write_config_dword(dev, pos, l);
  167. /*
  168. * All bits set in sz means the device isn't working properly.
  169. * If the BAR isn't implemented, all bits must be 0. If it's a
  170. * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
  171. * 1 must be clear.
  172. */
  173. if (sz == 0xffffffff)
  174. sz = 0;
  175. /*
  176. * I don't know how l can have all bits set. Copied from old code.
  177. * Maybe it fixes a bug on some ancient platform.
  178. */
  179. if (l == 0xffffffff)
  180. l = 0;
  181. if (type == pci_bar_unknown) {
  182. res->flags = decode_bar(dev, l);
  183. res->flags |= IORESOURCE_SIZEALIGN;
  184. if (res->flags & IORESOURCE_IO) {
  185. l64 = l & PCI_BASE_ADDRESS_IO_MASK;
  186. sz64 = sz & PCI_BASE_ADDRESS_IO_MASK;
  187. mask64 = PCI_BASE_ADDRESS_IO_MASK & (u32)IO_SPACE_LIMIT;
  188. } else {
  189. l64 = l & PCI_BASE_ADDRESS_MEM_MASK;
  190. sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
  191. mask64 = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  192. }
  193. } else {
  194. if (l & PCI_ROM_ADDRESS_ENABLE)
  195. res->flags |= IORESOURCE_ROM_ENABLE;
  196. l64 = l & PCI_ROM_ADDRESS_MASK;
  197. sz64 = sz & PCI_ROM_ADDRESS_MASK;
  198. mask64 = PCI_ROM_ADDRESS_MASK;
  199. }
  200. if (res->flags & IORESOURCE_MEM_64) {
  201. pci_read_config_dword(dev, pos + 4, &l);
  202. pci_write_config_dword(dev, pos + 4, ~0);
  203. pci_read_config_dword(dev, pos + 4, &sz);
  204. pci_write_config_dword(dev, pos + 4, l);
  205. l64 |= ((u64)l << 32);
  206. sz64 |= ((u64)sz << 32);
  207. mask64 |= ((u64)~0 << 32);
  208. }
  209. if (!dev->mmio_always_on && (orig_cmd & PCI_COMMAND_DECODE_ENABLE))
  210. pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
  211. if (!sz64)
  212. goto fail;
  213. sz64 = pci_size(l64, sz64, mask64);
  214. if (!sz64) {
  215. dev_info(&dev->dev, FW_BUG "reg 0x%x: invalid BAR (can't size)\n",
  216. pos);
  217. goto fail;
  218. }
  219. if (res->flags & IORESOURCE_MEM_64) {
  220. if ((sizeof(pci_bus_addr_t) < 8 || sizeof(resource_size_t) < 8)
  221. && sz64 > 0x100000000ULL) {
  222. res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  223. res->start = 0;
  224. res->end = 0;
  225. dev_err(&dev->dev, "reg 0x%x: can't handle BAR larger than 4GB (size %#010llx)\n",
  226. pos, (unsigned long long)sz64);
  227. goto out;
  228. }
  229. if ((sizeof(pci_bus_addr_t) < 8) && l) {
  230. /* Above 32-bit boundary; try to reallocate */
  231. res->flags |= IORESOURCE_UNSET;
  232. res->start = 0;
  233. res->end = sz64;
  234. dev_info(&dev->dev, "reg 0x%x: can't handle BAR above 4GB (bus address %#010llx)\n",
  235. pos, (unsigned long long)l64);
  236. goto out;
  237. }
  238. }
  239. region.start = l64;
  240. region.end = l64 + sz64;
  241. pcibios_bus_to_resource(dev->bus, res, &region);
  242. pcibios_resource_to_bus(dev->bus, &inverted_region, res);
  243. /*
  244. * If "A" is a BAR value (a bus address), "bus_to_resource(A)" is
  245. * the corresponding resource address (the physical address used by
  246. * the CPU. Converting that resource address back to a bus address
  247. * should yield the original BAR value:
  248. *
  249. * resource_to_bus(bus_to_resource(A)) == A
  250. *
  251. * If it doesn't, CPU accesses to "bus_to_resource(A)" will not
  252. * be claimed by the device.
  253. */
  254. if (inverted_region.start != region.start) {
  255. res->flags |= IORESOURCE_UNSET;
  256. res->start = 0;
  257. res->end = region.end - region.start;
  258. dev_info(&dev->dev, "reg 0x%x: initial BAR value %#010llx invalid\n",
  259. pos, (unsigned long long)region.start);
  260. }
  261. goto out;
  262. fail:
  263. res->flags = 0;
  264. out:
  265. if (res->flags)
  266. dev_printk(KERN_DEBUG, &dev->dev, "reg 0x%x: %pR\n", pos, res);
  267. return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  268. }
  269. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  270. {
  271. unsigned int pos, reg;
  272. if (dev->non_compliant_bars)
  273. return;
  274. for (pos = 0; pos < howmany; pos++) {
  275. struct resource *res = &dev->resource[pos];
  276. reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  277. pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
  278. }
  279. if (rom) {
  280. struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
  281. dev->rom_base_reg = rom;
  282. res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
  283. IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
  284. __pci_read_base(dev, pci_bar_mem32, res, rom);
  285. }
  286. }
  287. static void pci_read_bridge_io(struct pci_bus *child)
  288. {
  289. struct pci_dev *dev = child->self;
  290. u8 io_base_lo, io_limit_lo;
  291. unsigned long io_mask, io_granularity, base, limit;
  292. struct pci_bus_region region;
  293. struct resource *res;
  294. io_mask = PCI_IO_RANGE_MASK;
  295. io_granularity = 0x1000;
  296. if (dev->io_window_1k) {
  297. /* Support 1K I/O space granularity */
  298. io_mask = PCI_IO_1K_RANGE_MASK;
  299. io_granularity = 0x400;
  300. }
  301. res = child->resource[0];
  302. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  303. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  304. base = (io_base_lo & io_mask) << 8;
  305. limit = (io_limit_lo & io_mask) << 8;
  306. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  307. u16 io_base_hi, io_limit_hi;
  308. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  309. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  310. base |= ((unsigned long) io_base_hi << 16);
  311. limit |= ((unsigned long) io_limit_hi << 16);
  312. }
  313. if (base <= limit) {
  314. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  315. region.start = base;
  316. region.end = limit + io_granularity - 1;
  317. pcibios_bus_to_resource(dev->bus, res, &region);
  318. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  319. }
  320. }
  321. static void pci_read_bridge_mmio(struct pci_bus *child)
  322. {
  323. struct pci_dev *dev = child->self;
  324. u16 mem_base_lo, mem_limit_lo;
  325. unsigned long base, limit;
  326. struct pci_bus_region region;
  327. struct resource *res;
  328. res = child->resource[1];
  329. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  330. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  331. base = ((unsigned long) mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  332. limit = ((unsigned long) mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  333. if (base <= limit) {
  334. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  335. region.start = base;
  336. region.end = limit + 0xfffff;
  337. pcibios_bus_to_resource(dev->bus, res, &region);
  338. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  339. }
  340. }
  341. static void pci_read_bridge_mmio_pref(struct pci_bus *child)
  342. {
  343. struct pci_dev *dev = child->self;
  344. u16 mem_base_lo, mem_limit_lo;
  345. u64 base64, limit64;
  346. pci_bus_addr_t base, limit;
  347. struct pci_bus_region region;
  348. struct resource *res;
  349. res = child->resource[2];
  350. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  351. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  352. base64 = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  353. limit64 = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  354. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  355. u32 mem_base_hi, mem_limit_hi;
  356. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  357. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  358. /*
  359. * Some bridges set the base > limit by default, and some
  360. * (broken) BIOSes do not initialize them. If we find
  361. * this, just assume they are not being used.
  362. */
  363. if (mem_base_hi <= mem_limit_hi) {
  364. base64 |= (u64) mem_base_hi << 32;
  365. limit64 |= (u64) mem_limit_hi << 32;
  366. }
  367. }
  368. base = (pci_bus_addr_t) base64;
  369. limit = (pci_bus_addr_t) limit64;
  370. if (base != base64) {
  371. dev_err(&dev->dev, "can't handle bridge window above 4GB (bus address %#010llx)\n",
  372. (unsigned long long) base64);
  373. return;
  374. }
  375. if (base <= limit) {
  376. res->flags = (mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) |
  377. IORESOURCE_MEM | IORESOURCE_PREFETCH;
  378. if (res->flags & PCI_PREF_RANGE_TYPE_64)
  379. res->flags |= IORESOURCE_MEM_64;
  380. region.start = base;
  381. region.end = limit + 0xfffff;
  382. pcibios_bus_to_resource(dev->bus, res, &region);
  383. dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
  384. }
  385. }
  386. void pci_read_bridge_bases(struct pci_bus *child)
  387. {
  388. struct pci_dev *dev = child->self;
  389. struct resource *res;
  390. int i;
  391. if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
  392. return;
  393. dev_info(&dev->dev, "PCI bridge to %pR%s\n",
  394. &child->busn_res,
  395. dev->transparent ? " (subtractive decode)" : "");
  396. pci_bus_remove_resources(child);
  397. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  398. child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  399. pci_read_bridge_io(child);
  400. pci_read_bridge_mmio(child);
  401. pci_read_bridge_mmio_pref(child);
  402. if (dev->transparent) {
  403. pci_bus_for_each_resource(child->parent, res, i) {
  404. if (res && res->flags) {
  405. pci_bus_add_resource(child, res,
  406. PCI_SUBTRACTIVE_DECODE);
  407. dev_printk(KERN_DEBUG, &dev->dev,
  408. " bridge window %pR (subtractive decode)\n",
  409. res);
  410. }
  411. }
  412. }
  413. }
  414. static struct pci_bus *pci_alloc_bus(struct pci_bus *parent)
  415. {
  416. struct pci_bus *b;
  417. b = kzalloc(sizeof(*b), GFP_KERNEL);
  418. if (!b)
  419. return NULL;
  420. INIT_LIST_HEAD(&b->node);
  421. INIT_LIST_HEAD(&b->children);
  422. INIT_LIST_HEAD(&b->devices);
  423. INIT_LIST_HEAD(&b->slots);
  424. INIT_LIST_HEAD(&b->resources);
  425. b->max_bus_speed = PCI_SPEED_UNKNOWN;
  426. b->cur_bus_speed = PCI_SPEED_UNKNOWN;
  427. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  428. if (parent)
  429. b->domain_nr = parent->domain_nr;
  430. #endif
  431. return b;
  432. }
  433. static void pci_release_host_bridge_dev(struct device *dev)
  434. {
  435. struct pci_host_bridge *bridge = to_pci_host_bridge(dev);
  436. if (bridge->release_fn)
  437. bridge->release_fn(bridge);
  438. pci_free_resource_list(&bridge->windows);
  439. kfree(bridge);
  440. }
  441. static struct pci_host_bridge *pci_alloc_host_bridge(struct pci_bus *b)
  442. {
  443. struct pci_host_bridge *bridge;
  444. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  445. if (!bridge)
  446. return NULL;
  447. INIT_LIST_HEAD(&bridge->windows);
  448. bridge->bus = b;
  449. return bridge;
  450. }
  451. static const unsigned char pcix_bus_speed[] = {
  452. PCI_SPEED_UNKNOWN, /* 0 */
  453. PCI_SPEED_66MHz_PCIX, /* 1 */
  454. PCI_SPEED_100MHz_PCIX, /* 2 */
  455. PCI_SPEED_133MHz_PCIX, /* 3 */
  456. PCI_SPEED_UNKNOWN, /* 4 */
  457. PCI_SPEED_66MHz_PCIX_ECC, /* 5 */
  458. PCI_SPEED_100MHz_PCIX_ECC, /* 6 */
  459. PCI_SPEED_133MHz_PCIX_ECC, /* 7 */
  460. PCI_SPEED_UNKNOWN, /* 8 */
  461. PCI_SPEED_66MHz_PCIX_266, /* 9 */
  462. PCI_SPEED_100MHz_PCIX_266, /* A */
  463. PCI_SPEED_133MHz_PCIX_266, /* B */
  464. PCI_SPEED_UNKNOWN, /* C */
  465. PCI_SPEED_66MHz_PCIX_533, /* D */
  466. PCI_SPEED_100MHz_PCIX_533, /* E */
  467. PCI_SPEED_133MHz_PCIX_533 /* F */
  468. };
  469. const unsigned char pcie_link_speed[] = {
  470. PCI_SPEED_UNKNOWN, /* 0 */
  471. PCIE_SPEED_2_5GT, /* 1 */
  472. PCIE_SPEED_5_0GT, /* 2 */
  473. PCIE_SPEED_8_0GT, /* 3 */
  474. PCI_SPEED_UNKNOWN, /* 4 */
  475. PCI_SPEED_UNKNOWN, /* 5 */
  476. PCI_SPEED_UNKNOWN, /* 6 */
  477. PCI_SPEED_UNKNOWN, /* 7 */
  478. PCI_SPEED_UNKNOWN, /* 8 */
  479. PCI_SPEED_UNKNOWN, /* 9 */
  480. PCI_SPEED_UNKNOWN, /* A */
  481. PCI_SPEED_UNKNOWN, /* B */
  482. PCI_SPEED_UNKNOWN, /* C */
  483. PCI_SPEED_UNKNOWN, /* D */
  484. PCI_SPEED_UNKNOWN, /* E */
  485. PCI_SPEED_UNKNOWN /* F */
  486. };
  487. void pcie_update_link_speed(struct pci_bus *bus, u16 linksta)
  488. {
  489. bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
  490. }
  491. EXPORT_SYMBOL_GPL(pcie_update_link_speed);
  492. static unsigned char agp_speeds[] = {
  493. AGP_UNKNOWN,
  494. AGP_1X,
  495. AGP_2X,
  496. AGP_4X,
  497. AGP_8X
  498. };
  499. static enum pci_bus_speed agp_speed(int agp3, int agpstat)
  500. {
  501. int index = 0;
  502. if (agpstat & 4)
  503. index = 3;
  504. else if (agpstat & 2)
  505. index = 2;
  506. else if (agpstat & 1)
  507. index = 1;
  508. else
  509. goto out;
  510. if (agp3) {
  511. index += 2;
  512. if (index == 5)
  513. index = 0;
  514. }
  515. out:
  516. return agp_speeds[index];
  517. }
  518. static void pci_set_bus_speed(struct pci_bus *bus)
  519. {
  520. struct pci_dev *bridge = bus->self;
  521. int pos;
  522. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
  523. if (!pos)
  524. pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
  525. if (pos) {
  526. u32 agpstat, agpcmd;
  527. pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
  528. bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
  529. pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
  530. bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
  531. }
  532. pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  533. if (pos) {
  534. u16 status;
  535. enum pci_bus_speed max;
  536. pci_read_config_word(bridge, pos + PCI_X_BRIDGE_SSTATUS,
  537. &status);
  538. if (status & PCI_X_SSTATUS_533MHZ) {
  539. max = PCI_SPEED_133MHz_PCIX_533;
  540. } else if (status & PCI_X_SSTATUS_266MHZ) {
  541. max = PCI_SPEED_133MHz_PCIX_266;
  542. } else if (status & PCI_X_SSTATUS_133MHZ) {
  543. if ((status & PCI_X_SSTATUS_VERS) == PCI_X_SSTATUS_V2)
  544. max = PCI_SPEED_133MHz_PCIX_ECC;
  545. else
  546. max = PCI_SPEED_133MHz_PCIX;
  547. } else {
  548. max = PCI_SPEED_66MHz_PCIX;
  549. }
  550. bus->max_bus_speed = max;
  551. bus->cur_bus_speed = pcix_bus_speed[
  552. (status & PCI_X_SSTATUS_FREQ) >> 6];
  553. return;
  554. }
  555. if (pci_is_pcie(bridge)) {
  556. u32 linkcap;
  557. u16 linksta;
  558. pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
  559. bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
  560. pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
  561. pcie_update_link_speed(bus, linksta);
  562. }
  563. }
  564. static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
  565. {
  566. struct irq_domain *d;
  567. /*
  568. * Any firmware interface that can resolve the msi_domain
  569. * should be called from here.
  570. */
  571. d = pci_host_bridge_of_msi_domain(bus);
  572. if (!d)
  573. d = pci_host_bridge_acpi_msi_domain(bus);
  574. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  575. /*
  576. * If no IRQ domain was found via the OF tree, try looking it up
  577. * directly through the fwnode_handle.
  578. */
  579. if (!d) {
  580. struct fwnode_handle *fwnode = pci_root_bus_fwnode(bus);
  581. if (fwnode)
  582. d = irq_find_matching_fwnode(fwnode,
  583. DOMAIN_BUS_PCI_MSI);
  584. }
  585. #endif
  586. return d;
  587. }
  588. static void pci_set_bus_msi_domain(struct pci_bus *bus)
  589. {
  590. struct irq_domain *d;
  591. struct pci_bus *b;
  592. /*
  593. * The bus can be a root bus, a subordinate bus, or a virtual bus
  594. * created by an SR-IOV device. Walk up to the first bridge device
  595. * found or derive the domain from the host bridge.
  596. */
  597. for (b = bus, d = NULL; !d && !pci_is_root_bus(b); b = b->parent) {
  598. if (b->self)
  599. d = dev_get_msi_domain(&b->self->dev);
  600. }
  601. if (!d)
  602. d = pci_host_bridge_msi_domain(b);
  603. dev_set_msi_domain(&bus->dev, d);
  604. }
  605. static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  606. struct pci_dev *bridge, int busnr)
  607. {
  608. struct pci_bus *child;
  609. int i;
  610. int ret;
  611. /*
  612. * Allocate a new bus, and inherit stuff from the parent..
  613. */
  614. child = pci_alloc_bus(parent);
  615. if (!child)
  616. return NULL;
  617. child->parent = parent;
  618. child->ops = parent->ops;
  619. child->msi = parent->msi;
  620. child->sysdata = parent->sysdata;
  621. child->bus_flags = parent->bus_flags;
  622. /* initialize some portions of the bus device, but don't register it
  623. * now as the parent is not properly set up yet.
  624. */
  625. child->dev.class = &pcibus_class;
  626. dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
  627. /*
  628. * Set up the primary, secondary and subordinate
  629. * bus numbers.
  630. */
  631. child->number = child->busn_res.start = busnr;
  632. child->primary = parent->busn_res.start;
  633. child->busn_res.end = 0xff;
  634. if (!bridge) {
  635. child->dev.parent = parent->bridge;
  636. goto add_dev;
  637. }
  638. child->self = bridge;
  639. child->bridge = get_device(&bridge->dev);
  640. child->dev.parent = child->bridge;
  641. pci_set_bus_of_node(child);
  642. pci_set_bus_speed(child);
  643. /* Set up default resource pointers and names.. */
  644. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  645. child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  646. child->resource[i]->name = child->name;
  647. }
  648. bridge->subordinate = child;
  649. add_dev:
  650. pci_set_bus_msi_domain(child);
  651. ret = device_register(&child->dev);
  652. WARN_ON(ret < 0);
  653. pcibios_add_bus(child);
  654. if (child->ops->add_bus) {
  655. ret = child->ops->add_bus(child);
  656. if (WARN_ON(ret < 0))
  657. dev_err(&child->dev, "failed to add bus: %d\n", ret);
  658. }
  659. /* Create legacy_io and legacy_mem files for this bus */
  660. pci_create_legacy_files(child);
  661. return child;
  662. }
  663. struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
  664. int busnr)
  665. {
  666. struct pci_bus *child;
  667. child = pci_alloc_child_bus(parent, dev, busnr);
  668. if (child) {
  669. down_write(&pci_bus_sem);
  670. list_add_tail(&child->node, &parent->children);
  671. up_write(&pci_bus_sem);
  672. }
  673. return child;
  674. }
  675. EXPORT_SYMBOL(pci_add_new_bus);
  676. static void pci_enable_crs(struct pci_dev *pdev)
  677. {
  678. u16 root_cap = 0;
  679. /* Enable CRS Software Visibility if supported */
  680. pcie_capability_read_word(pdev, PCI_EXP_RTCAP, &root_cap);
  681. if (root_cap & PCI_EXP_RTCAP_CRSVIS)
  682. pcie_capability_set_word(pdev, PCI_EXP_RTCTL,
  683. PCI_EXP_RTCTL_CRSSVE);
  684. }
  685. /*
  686. * If it's a bridge, configure it and scan the bus behind it.
  687. * For CardBus bridges, we don't scan behind as the devices will
  688. * be handled by the bridge driver itself.
  689. *
  690. * We need to process bridges in two passes -- first we scan those
  691. * already configured by the BIOS and after we are done with all of
  692. * them, we proceed to assigning numbers to the remaining buses in
  693. * order to avoid overlaps between old and new bus numbers.
  694. */
  695. int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
  696. {
  697. struct pci_bus *child;
  698. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  699. u32 buses, i, j = 0;
  700. u16 bctl;
  701. u8 primary, secondary, subordinate;
  702. int broken = 0;
  703. /*
  704. * Make sure the bridge is powered on to be able to access config
  705. * space of devices below it.
  706. */
  707. pm_runtime_get_sync(&dev->dev);
  708. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  709. primary = buses & 0xFF;
  710. secondary = (buses >> 8) & 0xFF;
  711. subordinate = (buses >> 16) & 0xFF;
  712. dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
  713. secondary, subordinate, pass);
  714. if (!primary && (primary != bus->number) && secondary && subordinate) {
  715. dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
  716. primary = bus->number;
  717. }
  718. /* Check if setup is sensible at all */
  719. if (!pass &&
  720. (primary != bus->number || secondary <= bus->number ||
  721. secondary > subordinate)) {
  722. dev_info(&dev->dev, "bridge configuration invalid ([bus %02x-%02x]), reconfiguring\n",
  723. secondary, subordinate);
  724. broken = 1;
  725. }
  726. /* Disable MasterAbortMode during probing to avoid reporting
  727. of bus errors (in some architectures) */
  728. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  729. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  730. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  731. pci_enable_crs(dev);
  732. if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
  733. !is_cardbus && !broken) {
  734. unsigned int cmax;
  735. /*
  736. * Bus already configured by firmware, process it in the first
  737. * pass and just note the configuration.
  738. */
  739. if (pass)
  740. goto out;
  741. /*
  742. * The bus might already exist for two reasons: Either we are
  743. * rescanning the bus or the bus is reachable through more than
  744. * one bridge. The second case can happen with the i450NX
  745. * chipset.
  746. */
  747. child = pci_find_bus(pci_domain_nr(bus), secondary);
  748. if (!child) {
  749. child = pci_add_new_bus(bus, dev, secondary);
  750. if (!child)
  751. goto out;
  752. child->primary = primary;
  753. pci_bus_insert_busn_res(child, secondary, subordinate);
  754. child->bridge_ctl = bctl;
  755. }
  756. cmax = pci_scan_child_bus(child);
  757. if (cmax > subordinate)
  758. dev_warn(&dev->dev, "bridge has subordinate %02x but max busn %02x\n",
  759. subordinate, cmax);
  760. /* subordinate should equal child->busn_res.end */
  761. if (subordinate > max)
  762. max = subordinate;
  763. } else {
  764. /*
  765. * We need to assign a number to this bus which we always
  766. * do in the second pass.
  767. */
  768. if (!pass) {
  769. if (pcibios_assign_all_busses() || broken || is_cardbus)
  770. /* Temporarily disable forwarding of the
  771. configuration cycles on all bridges in
  772. this bus segment to avoid possible
  773. conflicts in the second pass between two
  774. bridges programmed with overlapping
  775. bus ranges. */
  776. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  777. buses & ~0xffffff);
  778. goto out;
  779. }
  780. /* Clear errors */
  781. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  782. /* Prevent assigning a bus number that already exists.
  783. * This can happen when a bridge is hot-plugged, so in
  784. * this case we only re-scan this bus. */
  785. child = pci_find_bus(pci_domain_nr(bus), max+1);
  786. if (!child) {
  787. child = pci_add_new_bus(bus, dev, max+1);
  788. if (!child)
  789. goto out;
  790. pci_bus_insert_busn_res(child, max+1,
  791. bus->busn_res.end);
  792. }
  793. max++;
  794. buses = (buses & 0xff000000)
  795. | ((unsigned int)(child->primary) << 0)
  796. | ((unsigned int)(child->busn_res.start) << 8)
  797. | ((unsigned int)(child->busn_res.end) << 16);
  798. /*
  799. * yenta.c forces a secondary latency timer of 176.
  800. * Copy that behaviour here.
  801. */
  802. if (is_cardbus) {
  803. buses &= ~0xff000000;
  804. buses |= CARDBUS_LATENCY_TIMER << 24;
  805. }
  806. /*
  807. * We need to blast all three values with a single write.
  808. */
  809. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  810. if (!is_cardbus) {
  811. child->bridge_ctl = bctl;
  812. max = pci_scan_child_bus(child);
  813. } else {
  814. /*
  815. * For CardBus bridges, we leave 4 bus numbers
  816. * as cards with a PCI-to-PCI bridge can be
  817. * inserted later.
  818. */
  819. for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) {
  820. struct pci_bus *parent = bus;
  821. if (pci_find_bus(pci_domain_nr(bus),
  822. max+i+1))
  823. break;
  824. while (parent->parent) {
  825. if ((!pcibios_assign_all_busses()) &&
  826. (parent->busn_res.end > max) &&
  827. (parent->busn_res.end <= max+i)) {
  828. j = 1;
  829. }
  830. parent = parent->parent;
  831. }
  832. if (j) {
  833. /*
  834. * Often, there are two cardbus bridges
  835. * -- try to leave one valid bus number
  836. * for each one.
  837. */
  838. i /= 2;
  839. break;
  840. }
  841. }
  842. max += i;
  843. }
  844. /*
  845. * Set the subordinate bus number to its real value.
  846. */
  847. pci_bus_update_busn_res_end(child, max);
  848. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  849. }
  850. sprintf(child->name,
  851. (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
  852. pci_domain_nr(bus), child->number);
  853. /* Has only triggered on CardBus, fixup is in yenta_socket */
  854. while (bus->parent) {
  855. if ((child->busn_res.end > bus->busn_res.end) ||
  856. (child->number > bus->busn_res.end) ||
  857. (child->number < bus->number) ||
  858. (child->busn_res.end < bus->number)) {
  859. dev_info(&child->dev, "%pR %s hidden behind%s bridge %s %pR\n",
  860. &child->busn_res,
  861. (bus->number > child->busn_res.end &&
  862. bus->busn_res.end < child->number) ?
  863. "wholly" : "partially",
  864. bus->self->transparent ? " transparent" : "",
  865. dev_name(&bus->dev),
  866. &bus->busn_res);
  867. }
  868. bus = bus->parent;
  869. }
  870. out:
  871. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  872. pm_runtime_put(&dev->dev);
  873. return max;
  874. }
  875. EXPORT_SYMBOL(pci_scan_bridge);
  876. /*
  877. * Read interrupt line and base address registers.
  878. * The architecture-dependent code can tweak these, of course.
  879. */
  880. static void pci_read_irq(struct pci_dev *dev)
  881. {
  882. unsigned char irq;
  883. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  884. dev->pin = irq;
  885. if (irq)
  886. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  887. dev->irq = irq;
  888. }
  889. void set_pcie_port_type(struct pci_dev *pdev)
  890. {
  891. int pos;
  892. u16 reg16;
  893. int type;
  894. struct pci_dev *parent;
  895. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  896. if (!pos)
  897. return;
  898. pdev->pcie_cap = pos;
  899. pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  900. pdev->pcie_flags_reg = reg16;
  901. pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
  902. pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
  903. /*
  904. * A Root Port or a PCI-to-PCIe bridge is always the upstream end
  905. * of a Link. No PCIe component has two Links. Two Links are
  906. * connected by a Switch that has a Port on each Link and internal
  907. * logic to connect the two Ports.
  908. */
  909. type = pci_pcie_type(pdev);
  910. if (type == PCI_EXP_TYPE_ROOT_PORT ||
  911. type == PCI_EXP_TYPE_PCIE_BRIDGE)
  912. pdev->has_secondary_link = 1;
  913. else if (type == PCI_EXP_TYPE_UPSTREAM ||
  914. type == PCI_EXP_TYPE_DOWNSTREAM) {
  915. parent = pci_upstream_bridge(pdev);
  916. /*
  917. * Usually there's an upstream device (Root Port or Switch
  918. * Downstream Port), but we can't assume one exists.
  919. */
  920. if (parent && !parent->has_secondary_link)
  921. pdev->has_secondary_link = 1;
  922. }
  923. }
  924. void set_pcie_hotplug_bridge(struct pci_dev *pdev)
  925. {
  926. u32 reg32;
  927. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32);
  928. if (reg32 & PCI_EXP_SLTCAP_HPC)
  929. pdev->is_hotplug_bridge = 1;
  930. }
  931. /**
  932. * pci_ext_cfg_is_aliased - is ext config space just an alias of std config?
  933. * @dev: PCI device
  934. *
  935. * PCI Express to PCI/PCI-X Bridge Specification, rev 1.0, 4.1.4 says that
  936. * when forwarding a type1 configuration request the bridge must check that
  937. * the extended register address field is zero. The bridge is not permitted
  938. * to forward the transactions and must handle it as an Unsupported Request.
  939. * Some bridges do not follow this rule and simply drop the extended register
  940. * bits, resulting in the standard config space being aliased, every 256
  941. * bytes across the entire configuration space. Test for this condition by
  942. * comparing the first dword of each potential alias to the vendor/device ID.
  943. * Known offenders:
  944. * ASM1083/1085 PCIe-to-PCI Reversible Bridge (1b21:1080, rev 01 & 03)
  945. * AMD/ATI SBx00 PCI to PCI Bridge (1002:4384, rev 40)
  946. */
  947. static bool pci_ext_cfg_is_aliased(struct pci_dev *dev)
  948. {
  949. #ifdef CONFIG_PCI_QUIRKS
  950. int pos;
  951. u32 header, tmp;
  952. pci_read_config_dword(dev, PCI_VENDOR_ID, &header);
  953. for (pos = PCI_CFG_SPACE_SIZE;
  954. pos < PCI_CFG_SPACE_EXP_SIZE; pos += PCI_CFG_SPACE_SIZE) {
  955. if (pci_read_config_dword(dev, pos, &tmp) != PCIBIOS_SUCCESSFUL
  956. || header != tmp)
  957. return false;
  958. }
  959. return true;
  960. #else
  961. return false;
  962. #endif
  963. }
  964. /**
  965. * pci_cfg_space_size - get the configuration space size of the PCI device.
  966. * @dev: PCI device
  967. *
  968. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  969. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  970. * access it. Maybe we don't have a way to generate extended config space
  971. * accesses, or the device is behind a reverse Express bridge. So we try
  972. * reading the dword at 0x100 which must either be 0 or a valid extended
  973. * capability header.
  974. */
  975. static int pci_cfg_space_size_ext(struct pci_dev *dev)
  976. {
  977. u32 status;
  978. int pos = PCI_CFG_SPACE_SIZE;
  979. if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
  980. return PCI_CFG_SPACE_SIZE;
  981. if (status == 0xffffffff || pci_ext_cfg_is_aliased(dev))
  982. return PCI_CFG_SPACE_SIZE;
  983. return PCI_CFG_SPACE_EXP_SIZE;
  984. }
  985. int pci_cfg_space_size(struct pci_dev *dev)
  986. {
  987. int pos;
  988. u32 status;
  989. u16 class;
  990. class = dev->class >> 8;
  991. if (class == PCI_CLASS_BRIDGE_HOST)
  992. return pci_cfg_space_size_ext(dev);
  993. if (pci_is_pcie(dev))
  994. return pci_cfg_space_size_ext(dev);
  995. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  996. if (!pos)
  997. return PCI_CFG_SPACE_SIZE;
  998. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  999. if (status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ))
  1000. return pci_cfg_space_size_ext(dev);
  1001. return PCI_CFG_SPACE_SIZE;
  1002. }
  1003. #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  1004. static void pci_msi_setup_pci_dev(struct pci_dev *dev)
  1005. {
  1006. /*
  1007. * Disable the MSI hardware to avoid screaming interrupts
  1008. * during boot. This is the power on reset default so
  1009. * usually this should be a noop.
  1010. */
  1011. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  1012. if (dev->msi_cap)
  1013. pci_msi_set_enable(dev, 0);
  1014. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  1015. if (dev->msix_cap)
  1016. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  1017. }
  1018. /**
  1019. * pci_setup_device - fill in class and map information of a device
  1020. * @dev: the device structure to fill
  1021. *
  1022. * Initialize the device structure with information about the device's
  1023. * vendor,class,memory and IO-space addresses,IRQ lines etc.
  1024. * Called at initialisation of the PCI subsystem and by CardBus services.
  1025. * Returns 0 on success and negative if unknown type of device (not normal,
  1026. * bridge or CardBus).
  1027. */
  1028. int pci_setup_device(struct pci_dev *dev)
  1029. {
  1030. u32 class;
  1031. u16 cmd;
  1032. u8 hdr_type;
  1033. int pos = 0;
  1034. struct pci_bus_region region;
  1035. struct resource *res;
  1036. if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
  1037. return -EIO;
  1038. dev->sysdata = dev->bus->sysdata;
  1039. dev->dev.parent = dev->bus->bridge;
  1040. dev->dev.bus = &pci_bus_type;
  1041. dev->hdr_type = hdr_type & 0x7f;
  1042. dev->multifunction = !!(hdr_type & 0x80);
  1043. dev->error_state = pci_channel_io_normal;
  1044. set_pcie_port_type(dev);
  1045. pci_dev_assign_slot(dev);
  1046. /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  1047. set this higher, assuming the system even supports it. */
  1048. dev->dma_mask = 0xffffffff;
  1049. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  1050. dev->bus->number, PCI_SLOT(dev->devfn),
  1051. PCI_FUNC(dev->devfn));
  1052. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  1053. dev->revision = class & 0xff;
  1054. dev->class = class >> 8; /* upper 3 bytes */
  1055. dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
  1056. dev->vendor, dev->device, dev->hdr_type, dev->class);
  1057. /* need to have dev->class ready */
  1058. dev->cfg_size = pci_cfg_space_size(dev);
  1059. /* "Unknown power state" */
  1060. dev->current_state = PCI_UNKNOWN;
  1061. /* Early fixups, before probing the BARs */
  1062. pci_fixup_device(pci_fixup_early, dev);
  1063. /* device class may be changed after fixup */
  1064. class = dev->class >> 8;
  1065. if (dev->non_compliant_bars) {
  1066. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1067. if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
  1068. dev_info(&dev->dev, "device has non-compliant BARs; disabling IO/MEM decoding\n");
  1069. cmd &= ~PCI_COMMAND_IO;
  1070. cmd &= ~PCI_COMMAND_MEMORY;
  1071. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1072. }
  1073. }
  1074. switch (dev->hdr_type) { /* header type */
  1075. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  1076. if (class == PCI_CLASS_BRIDGE_PCI)
  1077. goto bad;
  1078. pci_read_irq(dev);
  1079. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  1080. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1081. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  1082. /*
  1083. * Do the ugly legacy mode stuff here rather than broken chip
  1084. * quirk code. Legacy mode ATA controllers have fixed
  1085. * addresses. These are not always echoed in BAR0-3, and
  1086. * BAR0-3 in a few cases contain junk!
  1087. */
  1088. if (class == PCI_CLASS_STORAGE_IDE) {
  1089. u8 progif;
  1090. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1091. if ((progif & 1) == 0) {
  1092. region.start = 0x1F0;
  1093. region.end = 0x1F7;
  1094. res = &dev->resource[0];
  1095. res->flags = LEGACY_IO_RESOURCE;
  1096. pcibios_bus_to_resource(dev->bus, res, &region);
  1097. dev_info(&dev->dev, "legacy IDE quirk: reg 0x10: %pR\n",
  1098. res);
  1099. region.start = 0x3F6;
  1100. region.end = 0x3F6;
  1101. res = &dev->resource[1];
  1102. res->flags = LEGACY_IO_RESOURCE;
  1103. pcibios_bus_to_resource(dev->bus, res, &region);
  1104. dev_info(&dev->dev, "legacy IDE quirk: reg 0x14: %pR\n",
  1105. res);
  1106. }
  1107. if ((progif & 4) == 0) {
  1108. region.start = 0x170;
  1109. region.end = 0x177;
  1110. res = &dev->resource[2];
  1111. res->flags = LEGACY_IO_RESOURCE;
  1112. pcibios_bus_to_resource(dev->bus, res, &region);
  1113. dev_info(&dev->dev, "legacy IDE quirk: reg 0x18: %pR\n",
  1114. res);
  1115. region.start = 0x376;
  1116. region.end = 0x376;
  1117. res = &dev->resource[3];
  1118. res->flags = LEGACY_IO_RESOURCE;
  1119. pcibios_bus_to_resource(dev->bus, res, &region);
  1120. dev_info(&dev->dev, "legacy IDE quirk: reg 0x1c: %pR\n",
  1121. res);
  1122. }
  1123. }
  1124. break;
  1125. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  1126. if (class != PCI_CLASS_BRIDGE_PCI)
  1127. goto bad;
  1128. /* The PCI-to-PCI bridge spec requires that subtractive
  1129. decoding (i.e. transparent) bridge must have programming
  1130. interface code of 0x01. */
  1131. pci_read_irq(dev);
  1132. dev->transparent = ((dev->class & 0xff) == 1);
  1133. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  1134. set_pcie_hotplug_bridge(dev);
  1135. pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
  1136. if (pos) {
  1137. pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
  1138. pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
  1139. }
  1140. break;
  1141. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  1142. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  1143. goto bad;
  1144. pci_read_irq(dev);
  1145. pci_read_bases(dev, 1, 0);
  1146. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  1147. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  1148. break;
  1149. default: /* unknown header */
  1150. dev_err(&dev->dev, "unknown header type %02x, ignoring device\n",
  1151. dev->hdr_type);
  1152. return -EIO;
  1153. bad:
  1154. dev_err(&dev->dev, "ignoring class %#08x (doesn't match header type %02x)\n",
  1155. dev->class, dev->hdr_type);
  1156. dev->class = PCI_CLASS_NOT_DEFINED << 8;
  1157. }
  1158. /* We found a fine healthy device, go go go... */
  1159. return 0;
  1160. }
  1161. static void pci_configure_mps(struct pci_dev *dev)
  1162. {
  1163. struct pci_dev *bridge = pci_upstream_bridge(dev);
  1164. int mps, p_mps, rc;
  1165. if (!pci_is_pcie(dev) || !bridge || !pci_is_pcie(bridge))
  1166. return;
  1167. mps = pcie_get_mps(dev);
  1168. p_mps = pcie_get_mps(bridge);
  1169. if (mps == p_mps)
  1170. return;
  1171. if (pcie_bus_config == PCIE_BUS_TUNE_OFF) {
  1172. dev_warn(&dev->dev, "Max Payload Size %d, but upstream %s set to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1173. mps, pci_name(bridge), p_mps);
  1174. return;
  1175. }
  1176. /*
  1177. * Fancier MPS configuration is done later by
  1178. * pcie_bus_configure_settings()
  1179. */
  1180. if (pcie_bus_config != PCIE_BUS_DEFAULT)
  1181. return;
  1182. rc = pcie_set_mps(dev, p_mps);
  1183. if (rc) {
  1184. dev_warn(&dev->dev, "can't set Max Payload Size to %d; if necessary, use \"pci=pcie_bus_safe\" and report a bug\n",
  1185. p_mps);
  1186. return;
  1187. }
  1188. dev_info(&dev->dev, "Max Payload Size set to %d (was %d, max %d)\n",
  1189. p_mps, mps, 128 << dev->pcie_mpss);
  1190. }
  1191. static struct hpp_type0 pci_default_type0 = {
  1192. .revision = 1,
  1193. .cache_line_size = 8,
  1194. .latency_timer = 0x40,
  1195. .enable_serr = 0,
  1196. .enable_perr = 0,
  1197. };
  1198. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  1199. {
  1200. u16 pci_cmd, pci_bctl;
  1201. if (!hpp)
  1202. hpp = &pci_default_type0;
  1203. if (hpp->revision > 1) {
  1204. dev_warn(&dev->dev,
  1205. "PCI settings rev %d not supported; using defaults\n",
  1206. hpp->revision);
  1207. hpp = &pci_default_type0;
  1208. }
  1209. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  1210. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  1211. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1212. if (hpp->enable_serr)
  1213. pci_cmd |= PCI_COMMAND_SERR;
  1214. if (hpp->enable_perr)
  1215. pci_cmd |= PCI_COMMAND_PARITY;
  1216. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1217. /* Program bridge control value */
  1218. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1219. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1220. hpp->latency_timer);
  1221. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1222. if (hpp->enable_serr)
  1223. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1224. if (hpp->enable_perr)
  1225. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1226. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1227. }
  1228. }
  1229. static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
  1230. {
  1231. int pos;
  1232. if (!hpp)
  1233. return;
  1234. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1235. if (!pos)
  1236. return;
  1237. dev_warn(&dev->dev, "PCI-X settings not supported\n");
  1238. }
  1239. static bool pcie_root_rcb_set(struct pci_dev *dev)
  1240. {
  1241. struct pci_dev *rp = pcie_find_root_port(dev);
  1242. u16 lnkctl;
  1243. if (!rp)
  1244. return false;
  1245. pcie_capability_read_word(rp, PCI_EXP_LNKCTL, &lnkctl);
  1246. if (lnkctl & PCI_EXP_LNKCTL_RCB)
  1247. return true;
  1248. return false;
  1249. }
  1250. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  1251. {
  1252. int pos;
  1253. u32 reg32;
  1254. if (!hpp)
  1255. return;
  1256. if (!pci_is_pcie(dev))
  1257. return;
  1258. if (hpp->revision > 1) {
  1259. dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
  1260. hpp->revision);
  1261. return;
  1262. }
  1263. /*
  1264. * Don't allow _HPX to change MPS or MRRS settings. We manage
  1265. * those to make sure they're consistent with the rest of the
  1266. * platform.
  1267. */
  1268. hpp->pci_exp_devctl_and |= PCI_EXP_DEVCTL_PAYLOAD |
  1269. PCI_EXP_DEVCTL_READRQ;
  1270. hpp->pci_exp_devctl_or &= ~(PCI_EXP_DEVCTL_PAYLOAD |
  1271. PCI_EXP_DEVCTL_READRQ);
  1272. /* Initialize Device Control Register */
  1273. pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  1274. ~hpp->pci_exp_devctl_and, hpp->pci_exp_devctl_or);
  1275. /* Initialize Link Control Register */
  1276. if (pcie_cap_has_lnkctl(dev)) {
  1277. /*
  1278. * If the Root Port supports Read Completion Boundary of
  1279. * 128, set RCB to 128. Otherwise, clear it.
  1280. */
  1281. hpp->pci_exp_lnkctl_and |= PCI_EXP_LNKCTL_RCB;
  1282. hpp->pci_exp_lnkctl_or &= ~PCI_EXP_LNKCTL_RCB;
  1283. if (pcie_root_rcb_set(dev))
  1284. hpp->pci_exp_lnkctl_or |= PCI_EXP_LNKCTL_RCB;
  1285. pcie_capability_clear_and_set_word(dev, PCI_EXP_LNKCTL,
  1286. ~hpp->pci_exp_lnkctl_and, hpp->pci_exp_lnkctl_or);
  1287. }
  1288. /* Find Advanced Error Reporting Enhanced Capability */
  1289. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  1290. if (!pos)
  1291. return;
  1292. /* Initialize Uncorrectable Error Mask Register */
  1293. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  1294. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  1295. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  1296. /* Initialize Uncorrectable Error Severity Register */
  1297. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  1298. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  1299. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  1300. /* Initialize Correctable Error Mask Register */
  1301. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  1302. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  1303. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  1304. /* Initialize Advanced Error Capabilities and Control Register */
  1305. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  1306. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  1307. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  1308. /*
  1309. * FIXME: The following two registers are not supported yet.
  1310. *
  1311. * o Secondary Uncorrectable Error Severity Register
  1312. * o Secondary Uncorrectable Error Mask Register
  1313. */
  1314. }
  1315. static void pci_configure_device(struct pci_dev *dev)
  1316. {
  1317. struct hotplug_params hpp;
  1318. int ret;
  1319. pci_configure_mps(dev);
  1320. memset(&hpp, 0, sizeof(hpp));
  1321. ret = pci_get_hp_params(dev, &hpp);
  1322. if (ret)
  1323. return;
  1324. program_hpp_type2(dev, hpp.t2);
  1325. program_hpp_type1(dev, hpp.t1);
  1326. program_hpp_type0(dev, hpp.t0);
  1327. }
  1328. static void pci_release_capabilities(struct pci_dev *dev)
  1329. {
  1330. pci_vpd_release(dev);
  1331. pci_iov_release(dev);
  1332. pci_free_cap_save_buffers(dev);
  1333. }
  1334. /**
  1335. * pci_release_dev - free a pci device structure when all users of it are finished.
  1336. * @dev: device that's been disconnected
  1337. *
  1338. * Will be called only by the device core when all users of this pci device are
  1339. * done.
  1340. */
  1341. static void pci_release_dev(struct device *dev)
  1342. {
  1343. struct pci_dev *pci_dev;
  1344. pci_dev = to_pci_dev(dev);
  1345. pci_release_capabilities(pci_dev);
  1346. pci_release_of_node(pci_dev);
  1347. pcibios_release_device(pci_dev);
  1348. pci_bus_put(pci_dev->bus);
  1349. kfree(pci_dev->driver_override);
  1350. kfree(pci_dev->dma_alias_mask);
  1351. kfree(pci_dev);
  1352. }
  1353. struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
  1354. {
  1355. struct pci_dev *dev;
  1356. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1357. if (!dev)
  1358. return NULL;
  1359. INIT_LIST_HEAD(&dev->bus_list);
  1360. dev->dev.type = &pci_dev_type;
  1361. dev->bus = pci_bus_get(bus);
  1362. return dev;
  1363. }
  1364. EXPORT_SYMBOL(pci_alloc_dev);
  1365. bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
  1366. int crs_timeout)
  1367. {
  1368. int delay = 1;
  1369. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1370. return false;
  1371. /* some broken boards return 0 or ~0 if a slot is empty: */
  1372. if (*l == 0xffffffff || *l == 0x00000000 ||
  1373. *l == 0x0000ffff || *l == 0xffff0000)
  1374. return false;
  1375. /*
  1376. * Configuration Request Retry Status. Some root ports return the
  1377. * actual device ID instead of the synthetic ID (0xFFFF) required
  1378. * by the PCIe spec. Ignore the device ID and only check for
  1379. * (vendor id == 1).
  1380. */
  1381. while ((*l & 0xffff) == 0x0001) {
  1382. if (!crs_timeout)
  1383. return false;
  1384. msleep(delay);
  1385. delay *= 2;
  1386. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
  1387. return false;
  1388. /* Card hasn't responded in 60 seconds? Must be stuck. */
  1389. if (delay > crs_timeout) {
  1390. printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not responding\n",
  1391. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  1392. PCI_FUNC(devfn));
  1393. return false;
  1394. }
  1395. }
  1396. return true;
  1397. }
  1398. EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
  1399. /*
  1400. * Read the config data for a PCI device, sanity-check it
  1401. * and fill in the dev structure...
  1402. */
  1403. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  1404. {
  1405. struct pci_dev *dev;
  1406. u32 l;
  1407. if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
  1408. return NULL;
  1409. dev = pci_alloc_dev(bus);
  1410. if (!dev)
  1411. return NULL;
  1412. dev->devfn = devfn;
  1413. dev->vendor = l & 0xffff;
  1414. dev->device = (l >> 16) & 0xffff;
  1415. pci_set_of_node(dev);
  1416. if (pci_setup_device(dev)) {
  1417. pci_bus_put(dev->bus);
  1418. kfree(dev);
  1419. return NULL;
  1420. }
  1421. return dev;
  1422. }
  1423. static void pci_init_capabilities(struct pci_dev *dev)
  1424. {
  1425. /* Enhanced Allocation */
  1426. pci_ea_init(dev);
  1427. /* Setup MSI caps & disable MSI/MSI-X interrupts */
  1428. pci_msi_setup_pci_dev(dev);
  1429. /* Buffers for saving PCIe and PCI-X capabilities */
  1430. pci_allocate_cap_save_buffers(dev);
  1431. /* Power Management */
  1432. pci_pm_init(dev);
  1433. /* Vital Product Data */
  1434. pci_vpd_init(dev);
  1435. /* Alternative Routing-ID Forwarding */
  1436. pci_configure_ari(dev);
  1437. /* Single Root I/O Virtualization */
  1438. pci_iov_init(dev);
  1439. /* Address Translation Services */
  1440. pci_ats_init(dev);
  1441. /* Enable ACS P2P upstream forwarding */
  1442. pci_enable_acs(dev);
  1443. /* Precision Time Measurement */
  1444. pci_ptm_init(dev);
  1445. /* Advanced Error Reporting */
  1446. pci_aer_init(dev);
  1447. }
  1448. /*
  1449. * This is the equivalent of pci_host_bridge_msi_domain that acts on
  1450. * devices. Firmware interfaces that can select the MSI domain on a
  1451. * per-device basis should be called from here.
  1452. */
  1453. static struct irq_domain *pci_dev_msi_domain(struct pci_dev *dev)
  1454. {
  1455. struct irq_domain *d;
  1456. /*
  1457. * If a domain has been set through the pcibios_add_device
  1458. * callback, then this is the one (platform code knows best).
  1459. */
  1460. d = dev_get_msi_domain(&dev->dev);
  1461. if (d)
  1462. return d;
  1463. /*
  1464. * Let's see if we have a firmware interface able to provide
  1465. * the domain.
  1466. */
  1467. d = pci_msi_get_device_domain(dev);
  1468. if (d)
  1469. return d;
  1470. return NULL;
  1471. }
  1472. static void pci_set_msi_domain(struct pci_dev *dev)
  1473. {
  1474. struct irq_domain *d;
  1475. /*
  1476. * If the platform or firmware interfaces cannot supply a
  1477. * device-specific MSI domain, then inherit the default domain
  1478. * from the host bridge itself.
  1479. */
  1480. d = pci_dev_msi_domain(dev);
  1481. if (!d)
  1482. d = dev_get_msi_domain(&dev->bus->dev);
  1483. dev_set_msi_domain(&dev->dev, d);
  1484. }
  1485. /**
  1486. * pci_dma_configure - Setup DMA configuration
  1487. * @dev: ptr to pci_dev struct of the PCI device
  1488. *
  1489. * Function to update PCI devices's DMA configuration using the same
  1490. * info from the OF node or ACPI node of host bridge's parent (if any).
  1491. */
  1492. static void pci_dma_configure(struct pci_dev *dev)
  1493. {
  1494. struct device *bridge = pci_get_host_bridge_device(dev);
  1495. if (IS_ENABLED(CONFIG_OF) &&
  1496. bridge->parent && bridge->parent->of_node) {
  1497. of_dma_configure(&dev->dev, bridge->parent->of_node);
  1498. } else if (has_acpi_companion(bridge)) {
  1499. struct acpi_device *adev = to_acpi_device_node(bridge->fwnode);
  1500. enum dev_dma_attr attr = acpi_get_dma_attr(adev);
  1501. if (attr == DEV_DMA_NOT_SUPPORTED)
  1502. dev_warn(&dev->dev, "DMA not supported.\n");
  1503. else
  1504. arch_setup_dma_ops(&dev->dev, 0, 0, NULL,
  1505. attr == DEV_DMA_COHERENT);
  1506. }
  1507. pci_put_host_bridge_device(bridge);
  1508. }
  1509. void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  1510. {
  1511. int ret;
  1512. pci_configure_device(dev);
  1513. device_initialize(&dev->dev);
  1514. dev->dev.release = pci_release_dev;
  1515. set_dev_node(&dev->dev, pcibus_to_node(bus));
  1516. dev->dev.dma_mask = &dev->dma_mask;
  1517. dev->dev.dma_parms = &dev->dma_parms;
  1518. dev->dev.coherent_dma_mask = 0xffffffffull;
  1519. pci_dma_configure(dev);
  1520. pci_set_dma_max_seg_size(dev, 65536);
  1521. pci_set_dma_seg_boundary(dev, 0xffffffff);
  1522. /* Fix up broken headers */
  1523. pci_fixup_device(pci_fixup_header, dev);
  1524. /* moved out from quirk header fixup code */
  1525. pci_reassigndev_resource_alignment(dev);
  1526. /* Clear the state_saved flag. */
  1527. dev->state_saved = false;
  1528. /* Initialize various capabilities */
  1529. pci_init_capabilities(dev);
  1530. /*
  1531. * Add the device to our list of discovered devices
  1532. * and the bus list for fixup functions, etc.
  1533. */
  1534. down_write(&pci_bus_sem);
  1535. list_add_tail(&dev->bus_list, &bus->devices);
  1536. up_write(&pci_bus_sem);
  1537. ret = pcibios_add_device(dev);
  1538. WARN_ON(ret < 0);
  1539. /* Setup MSI irq domain */
  1540. pci_set_msi_domain(dev);
  1541. /* Notifier could use PCI capabilities */
  1542. dev->match_driver = false;
  1543. ret = device_add(&dev->dev);
  1544. WARN_ON(ret < 0);
  1545. }
  1546. struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
  1547. {
  1548. struct pci_dev *dev;
  1549. dev = pci_get_slot(bus, devfn);
  1550. if (dev) {
  1551. pci_dev_put(dev);
  1552. return dev;
  1553. }
  1554. dev = pci_scan_device(bus, devfn);
  1555. if (!dev)
  1556. return NULL;
  1557. pci_device_add(dev, bus);
  1558. return dev;
  1559. }
  1560. EXPORT_SYMBOL(pci_scan_single_device);
  1561. static unsigned next_fn(struct pci_bus *bus, struct pci_dev *dev, unsigned fn)
  1562. {
  1563. int pos;
  1564. u16 cap = 0;
  1565. unsigned next_fn;
  1566. if (pci_ari_enabled(bus)) {
  1567. if (!dev)
  1568. return 0;
  1569. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  1570. if (!pos)
  1571. return 0;
  1572. pci_read_config_word(dev, pos + PCI_ARI_CAP, &cap);
  1573. next_fn = PCI_ARI_CAP_NFN(cap);
  1574. if (next_fn <= fn)
  1575. return 0; /* protect against malformed list */
  1576. return next_fn;
  1577. }
  1578. /* dev may be NULL for non-contiguous multifunction devices */
  1579. if (!dev || dev->multifunction)
  1580. return (fn + 1) % 8;
  1581. return 0;
  1582. }
  1583. static int only_one_child(struct pci_bus *bus)
  1584. {
  1585. struct pci_dev *parent = bus->self;
  1586. if (!parent || !pci_is_pcie(parent))
  1587. return 0;
  1588. if (pci_pcie_type(parent) == PCI_EXP_TYPE_ROOT_PORT)
  1589. return 1;
  1590. /*
  1591. * PCIe downstream ports are bridges that normally lead to only a
  1592. * device 0, but if PCI_SCAN_ALL_PCIE_DEVS is set, scan all
  1593. * possible devices, not just device 0. See PCIe spec r3.0,
  1594. * sec 7.3.1.
  1595. */
  1596. if (parent->has_secondary_link &&
  1597. !pci_has_flag(PCI_SCAN_ALL_PCIE_DEVS))
  1598. return 1;
  1599. return 0;
  1600. }
  1601. /**
  1602. * pci_scan_slot - scan a PCI slot on a bus for devices.
  1603. * @bus: PCI bus to scan
  1604. * @devfn: slot number to scan (must have zero function.)
  1605. *
  1606. * Scan a PCI slot on the specified PCI bus for devices, adding
  1607. * discovered devices to the @bus->devices list. New devices
  1608. * will not have is_added set.
  1609. *
  1610. * Returns the number of new devices found.
  1611. */
  1612. int pci_scan_slot(struct pci_bus *bus, int devfn)
  1613. {
  1614. unsigned fn, nr = 0;
  1615. struct pci_dev *dev;
  1616. if (only_one_child(bus) && (devfn > 0))
  1617. return 0; /* Already scanned the entire slot */
  1618. dev = pci_scan_single_device(bus, devfn);
  1619. if (!dev)
  1620. return 0;
  1621. if (!dev->is_added)
  1622. nr++;
  1623. for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
  1624. dev = pci_scan_single_device(bus, devfn + fn);
  1625. if (dev) {
  1626. if (!dev->is_added)
  1627. nr++;
  1628. dev->multifunction = 1;
  1629. }
  1630. }
  1631. /* only one slot has pcie device */
  1632. if (bus->self && nr)
  1633. pcie_aspm_init_link_state(bus->self);
  1634. return nr;
  1635. }
  1636. EXPORT_SYMBOL(pci_scan_slot);
  1637. static int pcie_find_smpss(struct pci_dev *dev, void *data)
  1638. {
  1639. u8 *smpss = data;
  1640. if (!pci_is_pcie(dev))
  1641. return 0;
  1642. /*
  1643. * We don't have a way to change MPS settings on devices that have
  1644. * drivers attached. A hot-added device might support only the minimum
  1645. * MPS setting (MPS=128). Therefore, if the fabric contains a bridge
  1646. * where devices may be hot-added, we limit the fabric MPS to 128 so
  1647. * hot-added devices will work correctly.
  1648. *
  1649. * However, if we hot-add a device to a slot directly below a Root
  1650. * Port, it's impossible for there to be other existing devices below
  1651. * the port. We don't limit the MPS in this case because we can
  1652. * reconfigure MPS on both the Root Port and the hot-added device,
  1653. * and there are no other devices involved.
  1654. *
  1655. * Note that this PCIE_BUS_SAFE path assumes no peer-to-peer DMA.
  1656. */
  1657. if (dev->is_hotplug_bridge &&
  1658. pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT)
  1659. *smpss = 0;
  1660. if (*smpss > dev->pcie_mpss)
  1661. *smpss = dev->pcie_mpss;
  1662. return 0;
  1663. }
  1664. static void pcie_write_mps(struct pci_dev *dev, int mps)
  1665. {
  1666. int rc;
  1667. if (pcie_bus_config == PCIE_BUS_PERFORMANCE) {
  1668. mps = 128 << dev->pcie_mpss;
  1669. if (pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT &&
  1670. dev->bus->self)
  1671. /* For "Performance", the assumption is made that
  1672. * downstream communication will never be larger than
  1673. * the MRRS. So, the MPS only needs to be configured
  1674. * for the upstream communication. This being the case,
  1675. * walk from the top down and set the MPS of the child
  1676. * to that of the parent bus.
  1677. *
  1678. * Configure the device MPS with the smaller of the
  1679. * device MPSS or the bridge MPS (which is assumed to be
  1680. * properly configured at this point to the largest
  1681. * allowable MPS based on its parent bus).
  1682. */
  1683. mps = min(mps, pcie_get_mps(dev->bus->self));
  1684. }
  1685. rc = pcie_set_mps(dev, mps);
  1686. if (rc)
  1687. dev_err(&dev->dev, "Failed attempting to set the MPS\n");
  1688. }
  1689. static void pcie_write_mrrs(struct pci_dev *dev)
  1690. {
  1691. int rc, mrrs;
  1692. /* In the "safe" case, do not configure the MRRS. There appear to be
  1693. * issues with setting MRRS to 0 on a number of devices.
  1694. */
  1695. if (pcie_bus_config != PCIE_BUS_PERFORMANCE)
  1696. return;
  1697. /* For Max performance, the MRRS must be set to the largest supported
  1698. * value. However, it cannot be configured larger than the MPS the
  1699. * device or the bus can support. This should already be properly
  1700. * configured by a prior call to pcie_write_mps.
  1701. */
  1702. mrrs = pcie_get_mps(dev);
  1703. /* MRRS is a R/W register. Invalid values can be written, but a
  1704. * subsequent read will verify if the value is acceptable or not.
  1705. * If the MRRS value provided is not acceptable (e.g., too large),
  1706. * shrink the value until it is acceptable to the HW.
  1707. */
  1708. while (mrrs != pcie_get_readrq(dev) && mrrs >= 128) {
  1709. rc = pcie_set_readrq(dev, mrrs);
  1710. if (!rc)
  1711. break;
  1712. dev_warn(&dev->dev, "Failed attempting to set the MRRS\n");
  1713. mrrs /= 2;
  1714. }
  1715. if (mrrs < 128)
  1716. dev_err(&dev->dev, "MRRS was unable to be configured with a safe value. If problems are experienced, try running with pci=pcie_bus_safe\n");
  1717. }
  1718. static int pcie_bus_configure_set(struct pci_dev *dev, void *data)
  1719. {
  1720. int mps, orig_mps;
  1721. if (!pci_is_pcie(dev))
  1722. return 0;
  1723. if (pcie_bus_config == PCIE_BUS_TUNE_OFF ||
  1724. pcie_bus_config == PCIE_BUS_DEFAULT)
  1725. return 0;
  1726. mps = 128 << *(u8 *)data;
  1727. orig_mps = pcie_get_mps(dev);
  1728. pcie_write_mps(dev, mps);
  1729. pcie_write_mrrs(dev);
  1730. dev_info(&dev->dev, "Max Payload Size set to %4d/%4d (was %4d), Max Read Rq %4d\n",
  1731. pcie_get_mps(dev), 128 << dev->pcie_mpss,
  1732. orig_mps, pcie_get_readrq(dev));
  1733. return 0;
  1734. }
  1735. /* pcie_bus_configure_settings requires that pci_walk_bus work in a top-down,
  1736. * parents then children fashion. If this changes, then this code will not
  1737. * work as designed.
  1738. */
  1739. void pcie_bus_configure_settings(struct pci_bus *bus)
  1740. {
  1741. u8 smpss = 0;
  1742. if (!bus->self)
  1743. return;
  1744. if (!pci_is_pcie(bus->self))
  1745. return;
  1746. /* FIXME - Peer to peer DMA is possible, though the endpoint would need
  1747. * to be aware of the MPS of the destination. To work around this,
  1748. * simply force the MPS of the entire system to the smallest possible.
  1749. */
  1750. if (pcie_bus_config == PCIE_BUS_PEER2PEER)
  1751. smpss = 0;
  1752. if (pcie_bus_config == PCIE_BUS_SAFE) {
  1753. smpss = bus->self->pcie_mpss;
  1754. pcie_find_smpss(bus->self, &smpss);
  1755. pci_walk_bus(bus, pcie_find_smpss, &smpss);
  1756. }
  1757. pcie_bus_configure_set(bus->self, &smpss);
  1758. pci_walk_bus(bus, pcie_bus_configure_set, &smpss);
  1759. }
  1760. EXPORT_SYMBOL_GPL(pcie_bus_configure_settings);
  1761. unsigned int pci_scan_child_bus(struct pci_bus *bus)
  1762. {
  1763. unsigned int devfn, pass, max = bus->busn_res.start;
  1764. struct pci_dev *dev;
  1765. dev_dbg(&bus->dev, "scanning bus\n");
  1766. /* Go find them, Rover! */
  1767. for (devfn = 0; devfn < 0x100; devfn += 8)
  1768. pci_scan_slot(bus, devfn);
  1769. /* Reserve buses for SR-IOV capability. */
  1770. max += pci_iov_bus_range(bus);
  1771. /*
  1772. * After performing arch-dependent fixup of the bus, look behind
  1773. * all PCI-to-PCI bridges on this bus.
  1774. */
  1775. if (!bus->is_added) {
  1776. dev_dbg(&bus->dev, "fixups for bus\n");
  1777. pcibios_fixup_bus(bus);
  1778. bus->is_added = 1;
  1779. }
  1780. for (pass = 0; pass < 2; pass++)
  1781. list_for_each_entry(dev, &bus->devices, bus_list) {
  1782. if (pci_is_bridge(dev))
  1783. max = pci_scan_bridge(bus, dev, max, pass);
  1784. }
  1785. /*
  1786. * Make sure a hotplug bridge has at least the minimum requested
  1787. * number of buses.
  1788. */
  1789. if (bus->self && bus->self->is_hotplug_bridge && pci_hotplug_bus_size) {
  1790. if (max - bus->busn_res.start < pci_hotplug_bus_size - 1)
  1791. max = bus->busn_res.start + pci_hotplug_bus_size - 1;
  1792. /* Do not allocate more buses than we have room left */
  1793. if (max > bus->busn_res.end)
  1794. max = bus->busn_res.end;
  1795. }
  1796. /*
  1797. * We've scanned the bus and so we know all about what's on
  1798. * the other side of any bridges that may be on this bus plus
  1799. * any devices.
  1800. *
  1801. * Return how far we've got finding sub-buses.
  1802. */
  1803. dev_dbg(&bus->dev, "bus scan returning with max=%02x\n", max);
  1804. return max;
  1805. }
  1806. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  1807. /**
  1808. * pcibios_root_bridge_prepare - Platform-specific host bridge setup.
  1809. * @bridge: Host bridge to set up.
  1810. *
  1811. * Default empty implementation. Replace with an architecture-specific setup
  1812. * routine, if necessary.
  1813. */
  1814. int __weak pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  1815. {
  1816. return 0;
  1817. }
  1818. void __weak pcibios_add_bus(struct pci_bus *bus)
  1819. {
  1820. }
  1821. void __weak pcibios_remove_bus(struct pci_bus *bus)
  1822. {
  1823. }
  1824. struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
  1825. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  1826. {
  1827. int error;
  1828. struct pci_host_bridge *bridge;
  1829. struct pci_bus *b, *b2;
  1830. struct resource_entry *window, *n;
  1831. struct resource *res;
  1832. resource_size_t offset;
  1833. char bus_addr[64];
  1834. char *fmt;
  1835. b = pci_alloc_bus(NULL);
  1836. if (!b)
  1837. return NULL;
  1838. b->sysdata = sysdata;
  1839. b->ops = ops;
  1840. b->number = b->busn_res.start = bus;
  1841. #ifdef CONFIG_PCI_DOMAINS_GENERIC
  1842. b->domain_nr = pci_bus_find_domain_nr(b, parent);
  1843. #endif
  1844. b2 = pci_find_bus(pci_domain_nr(b), bus);
  1845. if (b2) {
  1846. /* If we already got to this bus through a different bridge, ignore it */
  1847. dev_dbg(&b2->dev, "bus already known\n");
  1848. goto err_out;
  1849. }
  1850. bridge = pci_alloc_host_bridge(b);
  1851. if (!bridge)
  1852. goto err_out;
  1853. bridge->dev.parent = parent;
  1854. bridge->dev.release = pci_release_host_bridge_dev;
  1855. dev_set_name(&bridge->dev, "pci%04x:%02x", pci_domain_nr(b), bus);
  1856. error = pcibios_root_bridge_prepare(bridge);
  1857. if (error) {
  1858. kfree(bridge);
  1859. goto err_out;
  1860. }
  1861. error = device_register(&bridge->dev);
  1862. if (error) {
  1863. put_device(&bridge->dev);
  1864. goto err_out;
  1865. }
  1866. b->bridge = get_device(&bridge->dev);
  1867. device_enable_async_suspend(b->bridge);
  1868. pci_set_bus_of_node(b);
  1869. pci_set_bus_msi_domain(b);
  1870. if (!parent)
  1871. set_dev_node(b->bridge, pcibus_to_node(b));
  1872. b->dev.class = &pcibus_class;
  1873. b->dev.parent = b->bridge;
  1874. dev_set_name(&b->dev, "%04x:%02x", pci_domain_nr(b), bus);
  1875. error = device_register(&b->dev);
  1876. if (error)
  1877. goto class_dev_reg_err;
  1878. pcibios_add_bus(b);
  1879. /* Create legacy_io and legacy_mem files for this bus */
  1880. pci_create_legacy_files(b);
  1881. if (parent)
  1882. dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev));
  1883. else
  1884. printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev));
  1885. /* Add initial resources to the bus */
  1886. resource_list_for_each_entry_safe(window, n, resources) {
  1887. list_move_tail(&window->node, &bridge->windows);
  1888. res = window->res;
  1889. offset = window->offset;
  1890. if (res->flags & IORESOURCE_BUS)
  1891. pci_bus_insert_busn_res(b, bus, res->end);
  1892. else
  1893. pci_bus_add_resource(b, res, 0);
  1894. if (offset) {
  1895. if (resource_type(res) == IORESOURCE_IO)
  1896. fmt = " (bus address [%#06llx-%#06llx])";
  1897. else
  1898. fmt = " (bus address [%#010llx-%#010llx])";
  1899. snprintf(bus_addr, sizeof(bus_addr), fmt,
  1900. (unsigned long long) (res->start - offset),
  1901. (unsigned long long) (res->end - offset));
  1902. } else
  1903. bus_addr[0] = '\0';
  1904. dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr);
  1905. }
  1906. down_write(&pci_bus_sem);
  1907. list_add_tail(&b->node, &pci_root_buses);
  1908. up_write(&pci_bus_sem);
  1909. return b;
  1910. class_dev_reg_err:
  1911. put_device(&bridge->dev);
  1912. device_unregister(&bridge->dev);
  1913. err_out:
  1914. kfree(b);
  1915. return NULL;
  1916. }
  1917. EXPORT_SYMBOL_GPL(pci_create_root_bus);
  1918. int pci_bus_insert_busn_res(struct pci_bus *b, int bus, int bus_max)
  1919. {
  1920. struct resource *res = &b->busn_res;
  1921. struct resource *parent_res, *conflict;
  1922. res->start = bus;
  1923. res->end = bus_max;
  1924. res->flags = IORESOURCE_BUS;
  1925. if (!pci_is_root_bus(b))
  1926. parent_res = &b->parent->busn_res;
  1927. else {
  1928. parent_res = get_pci_domain_busn_res(pci_domain_nr(b));
  1929. res->flags |= IORESOURCE_PCI_FIXED;
  1930. }
  1931. conflict = request_resource_conflict(parent_res, res);
  1932. if (conflict)
  1933. dev_printk(KERN_DEBUG, &b->dev,
  1934. "busn_res: can not insert %pR under %s%pR (conflicts with %s %pR)\n",
  1935. res, pci_is_root_bus(b) ? "domain " : "",
  1936. parent_res, conflict->name, conflict);
  1937. return conflict == NULL;
  1938. }
  1939. int pci_bus_update_busn_res_end(struct pci_bus *b, int bus_max)
  1940. {
  1941. struct resource *res = &b->busn_res;
  1942. struct resource old_res = *res;
  1943. resource_size_t size;
  1944. int ret;
  1945. if (res->start > bus_max)
  1946. return -EINVAL;
  1947. size = bus_max - res->start + 1;
  1948. ret = adjust_resource(res, res->start, size);
  1949. dev_printk(KERN_DEBUG, &b->dev,
  1950. "busn_res: %pR end %s updated to %02x\n",
  1951. &old_res, ret ? "can not be" : "is", bus_max);
  1952. if (!ret && !res->parent)
  1953. pci_bus_insert_busn_res(b, res->start, res->end);
  1954. return ret;
  1955. }
  1956. void pci_bus_release_busn_res(struct pci_bus *b)
  1957. {
  1958. struct resource *res = &b->busn_res;
  1959. int ret;
  1960. if (!res->flags || !res->parent)
  1961. return;
  1962. ret = release_resource(res);
  1963. dev_printk(KERN_DEBUG, &b->dev,
  1964. "busn_res: %pR %s released\n",
  1965. res, ret ? "can not be" : "is");
  1966. }
  1967. struct pci_bus *pci_scan_root_bus_msi(struct device *parent, int bus,
  1968. struct pci_ops *ops, void *sysdata,
  1969. struct list_head *resources, struct msi_controller *msi)
  1970. {
  1971. struct resource_entry *window;
  1972. bool found = false;
  1973. struct pci_bus *b;
  1974. int max;
  1975. resource_list_for_each_entry(window, resources)
  1976. if (window->res->flags & IORESOURCE_BUS) {
  1977. found = true;
  1978. break;
  1979. }
  1980. b = pci_create_root_bus(parent, bus, ops, sysdata, resources);
  1981. if (!b)
  1982. return NULL;
  1983. b->msi = msi;
  1984. if (!found) {
  1985. dev_info(&b->dev,
  1986. "No busn resource found for root bus, will use [bus %02x-ff]\n",
  1987. bus);
  1988. pci_bus_insert_busn_res(b, bus, 255);
  1989. }
  1990. max = pci_scan_child_bus(b);
  1991. if (!found)
  1992. pci_bus_update_busn_res_end(b, max);
  1993. return b;
  1994. }
  1995. struct pci_bus *pci_scan_root_bus(struct device *parent, int bus,
  1996. struct pci_ops *ops, void *sysdata, struct list_head *resources)
  1997. {
  1998. return pci_scan_root_bus_msi(parent, bus, ops, sysdata, resources,
  1999. NULL);
  2000. }
  2001. EXPORT_SYMBOL(pci_scan_root_bus);
  2002. struct pci_bus *pci_scan_bus(int bus, struct pci_ops *ops,
  2003. void *sysdata)
  2004. {
  2005. LIST_HEAD(resources);
  2006. struct pci_bus *b;
  2007. pci_add_resource(&resources, &ioport_resource);
  2008. pci_add_resource(&resources, &iomem_resource);
  2009. pci_add_resource(&resources, &busn_resource);
  2010. b = pci_create_root_bus(NULL, bus, ops, sysdata, &resources);
  2011. if (b) {
  2012. pci_scan_child_bus(b);
  2013. } else {
  2014. pci_free_resource_list(&resources);
  2015. }
  2016. return b;
  2017. }
  2018. EXPORT_SYMBOL(pci_scan_bus);
  2019. /**
  2020. * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
  2021. * @bridge: PCI bridge for the bus to scan
  2022. *
  2023. * Scan a PCI bus and child buses for new devices, add them,
  2024. * and enable them, resizing bridge mmio/io resource if necessary
  2025. * and possible. The caller must ensure the child devices are already
  2026. * removed for resizing to occur.
  2027. *
  2028. * Returns the max number of subordinate bus discovered.
  2029. */
  2030. unsigned int pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
  2031. {
  2032. unsigned int max;
  2033. struct pci_bus *bus = bridge->subordinate;
  2034. max = pci_scan_child_bus(bus);
  2035. pci_assign_unassigned_bridge_resources(bridge);
  2036. pci_bus_add_devices(bus);
  2037. return max;
  2038. }
  2039. /**
  2040. * pci_rescan_bus - scan a PCI bus for devices.
  2041. * @bus: PCI bus to scan
  2042. *
  2043. * Scan a PCI bus and child buses for new devices, adds them,
  2044. * and enables them.
  2045. *
  2046. * Returns the max number of subordinate bus discovered.
  2047. */
  2048. unsigned int pci_rescan_bus(struct pci_bus *bus)
  2049. {
  2050. unsigned int max;
  2051. max = pci_scan_child_bus(bus);
  2052. pci_assign_unassigned_bus_resources(bus);
  2053. pci_bus_add_devices(bus);
  2054. return max;
  2055. }
  2056. EXPORT_SYMBOL_GPL(pci_rescan_bus);
  2057. /*
  2058. * pci_rescan_bus(), pci_rescan_bus_bridge_resize() and PCI device removal
  2059. * routines should always be executed under this mutex.
  2060. */
  2061. static DEFINE_MUTEX(pci_rescan_remove_lock);
  2062. void pci_lock_rescan_remove(void)
  2063. {
  2064. mutex_lock(&pci_rescan_remove_lock);
  2065. }
  2066. EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
  2067. void pci_unlock_rescan_remove(void)
  2068. {
  2069. mutex_unlock(&pci_rescan_remove_lock);
  2070. }
  2071. EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
  2072. static int __init pci_sort_bf_cmp(const struct device *d_a,
  2073. const struct device *d_b)
  2074. {
  2075. const struct pci_dev *a = to_pci_dev(d_a);
  2076. const struct pci_dev *b = to_pci_dev(d_b);
  2077. if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
  2078. else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
  2079. if (a->bus->number < b->bus->number) return -1;
  2080. else if (a->bus->number > b->bus->number) return 1;
  2081. if (a->devfn < b->devfn) return -1;
  2082. else if (a->devfn > b->devfn) return 1;
  2083. return 0;
  2084. }
  2085. void __init pci_sort_breadthfirst(void)
  2086. {
  2087. bus_sort_breadthfirst(&pci_bus_type, &pci_sort_bf_cmp);
  2088. }