probe.c 77 KB

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