probe.c 84 KB

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