setup-bus.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support routines for initializing a PCI subsystem
  4. *
  5. * Extruded from code written by
  6. * Dave Rusling (david.rusling@reo.mts.dec.com)
  7. * David Mosberger (davidm@cs.arizona.edu)
  8. * David Miller (davem@redhat.com)
  9. *
  10. * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  11. * PCI-PCI bridges cleanup, sorted resource allocation.
  12. * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  13. * Converted to allocation in 3 passes, which gives
  14. * tighter packing. Prefetchable range support.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/cache.h>
  24. #include <linux/limits.h>
  25. #include <linux/sizes.h>
  26. #include <linux/slab.h>
  27. #include <linux/acpi.h>
  28. #include "pci.h"
  29. unsigned int pci_flags;
  30. EXPORT_SYMBOL_GPL(pci_flags);
  31. struct pci_dev_resource {
  32. struct list_head list;
  33. struct resource *res;
  34. struct pci_dev *dev;
  35. resource_size_t start;
  36. resource_size_t end;
  37. resource_size_t add_size;
  38. resource_size_t min_align;
  39. unsigned long flags;
  40. };
  41. static void free_list(struct list_head *head)
  42. {
  43. struct pci_dev_resource *dev_res, *tmp;
  44. list_for_each_entry_safe(dev_res, tmp, head, list) {
  45. list_del(&dev_res->list);
  46. kfree(dev_res);
  47. }
  48. }
  49. /**
  50. * add_to_list() - Add a new resource tracker to the list
  51. * @head: Head of the list
  52. * @dev: Device to which the resource belongs
  53. * @res: Resource to be tracked
  54. * @add_size: Additional size to be optionally added to the resource
  55. * @min_align: Minimum memory window alignment
  56. */
  57. static int add_to_list(struct list_head *head, struct pci_dev *dev,
  58. struct resource *res, resource_size_t add_size,
  59. resource_size_t min_align)
  60. {
  61. struct pci_dev_resource *tmp;
  62. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  63. if (!tmp)
  64. return -ENOMEM;
  65. tmp->res = res;
  66. tmp->dev = dev;
  67. tmp->start = res->start;
  68. tmp->end = res->end;
  69. tmp->flags = res->flags;
  70. tmp->add_size = add_size;
  71. tmp->min_align = min_align;
  72. list_add(&tmp->list, head);
  73. return 0;
  74. }
  75. static void remove_from_list(struct list_head *head, struct resource *res)
  76. {
  77. struct pci_dev_resource *dev_res, *tmp;
  78. list_for_each_entry_safe(dev_res, tmp, head, list) {
  79. if (dev_res->res == res) {
  80. list_del(&dev_res->list);
  81. kfree(dev_res);
  82. break;
  83. }
  84. }
  85. }
  86. static struct pci_dev_resource *res_to_dev_res(struct list_head *head,
  87. struct resource *res)
  88. {
  89. struct pci_dev_resource *dev_res;
  90. list_for_each_entry(dev_res, head, list) {
  91. if (dev_res->res == res)
  92. return dev_res;
  93. }
  94. return NULL;
  95. }
  96. static resource_size_t get_res_add_size(struct list_head *head,
  97. struct resource *res)
  98. {
  99. struct pci_dev_resource *dev_res;
  100. dev_res = res_to_dev_res(head, res);
  101. return dev_res ? dev_res->add_size : 0;
  102. }
  103. static resource_size_t get_res_add_align(struct list_head *head,
  104. struct resource *res)
  105. {
  106. struct pci_dev_resource *dev_res;
  107. dev_res = res_to_dev_res(head, res);
  108. return dev_res ? dev_res->min_align : 0;
  109. }
  110. /* Sort resources by alignment */
  111. static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
  112. {
  113. struct resource *r;
  114. int i;
  115. pci_dev_for_each_resource(dev, r, i) {
  116. struct pci_dev_resource *dev_res, *tmp;
  117. resource_size_t r_align;
  118. struct list_head *n;
  119. if (r->flags & IORESOURCE_PCI_FIXED)
  120. continue;
  121. if (!(r->flags) || r->parent)
  122. continue;
  123. r_align = pci_resource_alignment(dev, r);
  124. if (!r_align) {
  125. pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
  126. i, r);
  127. continue;
  128. }
  129. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  130. if (!tmp)
  131. panic("%s: kzalloc() failed!\n", __func__);
  132. tmp->res = r;
  133. tmp->dev = dev;
  134. /* Fallback is smallest one or list is empty */
  135. n = head;
  136. list_for_each_entry(dev_res, head, list) {
  137. resource_size_t align;
  138. align = pci_resource_alignment(dev_res->dev,
  139. dev_res->res);
  140. if (r_align > align) {
  141. n = &dev_res->list;
  142. break;
  143. }
  144. }
  145. /* Insert it just before n */
  146. list_add_tail(&tmp->list, n);
  147. }
  148. }
  149. static void __dev_sort_resources(struct pci_dev *dev, struct list_head *head)
  150. {
  151. u16 class = dev->class >> 8;
  152. /* Don't touch classless devices or host bridges or IOAPICs */
  153. if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
  154. return;
  155. /* Don't touch IOAPIC devices already enabled by firmware */
  156. if (class == PCI_CLASS_SYSTEM_PIC) {
  157. u16 command;
  158. pci_read_config_word(dev, PCI_COMMAND, &command);
  159. if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  160. return;
  161. }
  162. pdev_sort_resources(dev, head);
  163. }
  164. static inline void reset_resource(struct resource *res)
  165. {
  166. res->start = 0;
  167. res->end = 0;
  168. res->flags = 0;
  169. }
  170. /**
  171. * reassign_resources_sorted() - Satisfy any additional resource requests
  172. *
  173. * @realloc_head: Head of the list tracking requests requiring
  174. * additional resources
  175. * @head: Head of the list tracking requests with allocated
  176. * resources
  177. *
  178. * Walk through each element of the realloc_head and try to procure additional
  179. * resources for the element, provided the element is in the head list.
  180. */
  181. static void reassign_resources_sorted(struct list_head *realloc_head,
  182. struct list_head *head)
  183. {
  184. struct resource *res;
  185. const char *res_name;
  186. struct pci_dev_resource *add_res, *tmp;
  187. struct pci_dev_resource *dev_res;
  188. resource_size_t add_size, align;
  189. int idx;
  190. list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
  191. bool found_match = false;
  192. res = add_res->res;
  193. /* Skip resource that has been reset */
  194. if (!res->flags)
  195. goto out;
  196. /* Skip this resource if not found in head list */
  197. list_for_each_entry(dev_res, head, list) {
  198. if (dev_res->res == res) {
  199. found_match = true;
  200. break;
  201. }
  202. }
  203. if (!found_match) /* Just skip */
  204. continue;
  205. idx = res - &add_res->dev->resource[0];
  206. res_name = pci_resource_name(add_res->dev, idx);
  207. add_size = add_res->add_size;
  208. align = add_res->min_align;
  209. if (!resource_size(res)) {
  210. res->start = align;
  211. res->end = res->start + add_size - 1;
  212. if (pci_assign_resource(add_res->dev, idx))
  213. reset_resource(res);
  214. } else {
  215. res->flags |= add_res->flags &
  216. (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
  217. if (pci_reassign_resource(add_res->dev, idx,
  218. add_size, align))
  219. pci_info(add_res->dev, "%s %pR: failed to add %llx\n",
  220. res_name, res,
  221. (unsigned long long) add_size);
  222. }
  223. out:
  224. list_del(&add_res->list);
  225. kfree(add_res);
  226. }
  227. }
  228. /**
  229. * assign_requested_resources_sorted() - Satisfy resource requests
  230. *
  231. * @head: Head of the list tracking requests for resources
  232. * @fail_head: Head of the list tracking requests that could not be
  233. * allocated
  234. *
  235. * Satisfy resource requests of each element in the list. Add requests that
  236. * could not be satisfied to the failed_list.
  237. */
  238. static void assign_requested_resources_sorted(struct list_head *head,
  239. struct list_head *fail_head)
  240. {
  241. struct resource *res;
  242. struct pci_dev_resource *dev_res;
  243. int idx;
  244. list_for_each_entry(dev_res, head, list) {
  245. res = dev_res->res;
  246. idx = res - &dev_res->dev->resource[0];
  247. if (resource_size(res) &&
  248. pci_assign_resource(dev_res->dev, idx)) {
  249. if (fail_head) {
  250. /*
  251. * If the failed resource is a ROM BAR and
  252. * it will be enabled later, don't add it
  253. * to the list.
  254. */
  255. if (!((idx == PCI_ROM_RESOURCE) &&
  256. (!(res->flags & IORESOURCE_ROM_ENABLE))))
  257. add_to_list(fail_head,
  258. dev_res->dev, res,
  259. 0 /* don't care */,
  260. 0 /* don't care */);
  261. }
  262. reset_resource(res);
  263. }
  264. }
  265. }
  266. static unsigned long pci_fail_res_type_mask(struct list_head *fail_head)
  267. {
  268. struct pci_dev_resource *fail_res;
  269. unsigned long mask = 0;
  270. /* Check failed type */
  271. list_for_each_entry(fail_res, fail_head, list)
  272. mask |= fail_res->flags;
  273. /*
  274. * One pref failed resource will set IORESOURCE_MEM, as we can
  275. * allocate pref in non-pref range. Will release all assigned
  276. * non-pref sibling resources according to that bit.
  277. */
  278. return mask & (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH);
  279. }
  280. static bool pci_need_to_release(unsigned long mask, struct resource *res)
  281. {
  282. if (res->flags & IORESOURCE_IO)
  283. return !!(mask & IORESOURCE_IO);
  284. /* Check pref at first */
  285. if (res->flags & IORESOURCE_PREFETCH) {
  286. if (mask & IORESOURCE_PREFETCH)
  287. return true;
  288. /* Count pref if its parent is non-pref */
  289. else if ((mask & IORESOURCE_MEM) &&
  290. !(res->parent->flags & IORESOURCE_PREFETCH))
  291. return true;
  292. else
  293. return false;
  294. }
  295. if (res->flags & IORESOURCE_MEM)
  296. return !!(mask & IORESOURCE_MEM);
  297. return false; /* Should not get here */
  298. }
  299. static void __assign_resources_sorted(struct list_head *head,
  300. struct list_head *realloc_head,
  301. struct list_head *fail_head)
  302. {
  303. /*
  304. * Should not assign requested resources at first. They could be
  305. * adjacent, so later reassign can not reallocate them one by one in
  306. * parent resource window.
  307. *
  308. * Try to assign requested + add_size at beginning. If could do that,
  309. * could get out early. If could not do that, we still try to assign
  310. * requested at first, then try to reassign add_size for some resources.
  311. *
  312. * Separate three resource type checking if we need to release
  313. * assigned resource after requested + add_size try.
  314. *
  315. * 1. If IO port assignment fails, will release assigned IO
  316. * port.
  317. * 2. If pref MMIO assignment fails, release assigned pref
  318. * MMIO. If assigned pref MMIO's parent is non-pref MMIO
  319. * and non-pref MMIO assignment fails, will release that
  320. * assigned pref MMIO.
  321. * 3. If non-pref MMIO assignment fails or pref MMIO
  322. * assignment fails, will release assigned non-pref MMIO.
  323. */
  324. LIST_HEAD(save_head);
  325. LIST_HEAD(local_fail_head);
  326. struct pci_dev_resource *save_res;
  327. struct pci_dev_resource *dev_res, *tmp_res, *dev_res2;
  328. unsigned long fail_type;
  329. resource_size_t add_align, align;
  330. /* Check if optional add_size is there */
  331. if (!realloc_head || list_empty(realloc_head))
  332. goto requested_and_reassign;
  333. /* Save original start, end, flags etc at first */
  334. list_for_each_entry(dev_res, head, list) {
  335. if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
  336. free_list(&save_head);
  337. goto requested_and_reassign;
  338. }
  339. }
  340. /* Update res in head list with add_size in realloc_head list */
  341. list_for_each_entry_safe(dev_res, tmp_res, head, list) {
  342. dev_res->res->end += get_res_add_size(realloc_head,
  343. dev_res->res);
  344. /*
  345. * There are two kinds of additional resources in the list:
  346. * 1. bridge resource -- IORESOURCE_STARTALIGN
  347. * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN
  348. * Here just fix the additional alignment for bridge
  349. */
  350. if (!(dev_res->res->flags & IORESOURCE_STARTALIGN))
  351. continue;
  352. add_align = get_res_add_align(realloc_head, dev_res->res);
  353. /*
  354. * The "head" list is sorted by alignment so resources with
  355. * bigger alignment will be assigned first. After we
  356. * change the alignment of a dev_res in "head" list, we
  357. * need to reorder the list by alignment to make it
  358. * consistent.
  359. */
  360. if (add_align > dev_res->res->start) {
  361. resource_size_t r_size = resource_size(dev_res->res);
  362. dev_res->res->start = add_align;
  363. dev_res->res->end = add_align + r_size - 1;
  364. list_for_each_entry(dev_res2, head, list) {
  365. align = pci_resource_alignment(dev_res2->dev,
  366. dev_res2->res);
  367. if (add_align > align) {
  368. list_move_tail(&dev_res->list,
  369. &dev_res2->list);
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. /* Try updated head list with add_size added */
  376. assign_requested_resources_sorted(head, &local_fail_head);
  377. /* All assigned with add_size? */
  378. if (list_empty(&local_fail_head)) {
  379. /* Remove head list from realloc_head list */
  380. list_for_each_entry(dev_res, head, list)
  381. remove_from_list(realloc_head, dev_res->res);
  382. free_list(&save_head);
  383. free_list(head);
  384. return;
  385. }
  386. /* Check failed type */
  387. fail_type = pci_fail_res_type_mask(&local_fail_head);
  388. /* Remove not need to be released assigned res from head list etc */
  389. list_for_each_entry_safe(dev_res, tmp_res, head, list)
  390. if (dev_res->res->parent &&
  391. !pci_need_to_release(fail_type, dev_res->res)) {
  392. /* Remove it from realloc_head list */
  393. remove_from_list(realloc_head, dev_res->res);
  394. remove_from_list(&save_head, dev_res->res);
  395. list_del(&dev_res->list);
  396. kfree(dev_res);
  397. }
  398. free_list(&local_fail_head);
  399. /* Release assigned resource */
  400. list_for_each_entry(dev_res, head, list)
  401. if (dev_res->res->parent)
  402. release_resource(dev_res->res);
  403. /* Restore start/end/flags from saved list */
  404. list_for_each_entry(save_res, &save_head, list) {
  405. struct resource *res = save_res->res;
  406. res->start = save_res->start;
  407. res->end = save_res->end;
  408. res->flags = save_res->flags;
  409. }
  410. free_list(&save_head);
  411. requested_and_reassign:
  412. /* Satisfy the must-have resource requests */
  413. assign_requested_resources_sorted(head, fail_head);
  414. /* Try to satisfy any additional optional resource requests */
  415. if (realloc_head)
  416. reassign_resources_sorted(realloc_head, head);
  417. free_list(head);
  418. }
  419. static void pdev_assign_resources_sorted(struct pci_dev *dev,
  420. struct list_head *add_head,
  421. struct list_head *fail_head)
  422. {
  423. LIST_HEAD(head);
  424. __dev_sort_resources(dev, &head);
  425. __assign_resources_sorted(&head, add_head, fail_head);
  426. }
  427. static void pbus_assign_resources_sorted(const struct pci_bus *bus,
  428. struct list_head *realloc_head,
  429. struct list_head *fail_head)
  430. {
  431. struct pci_dev *dev;
  432. LIST_HEAD(head);
  433. list_for_each_entry(dev, &bus->devices, bus_list)
  434. __dev_sort_resources(dev, &head);
  435. __assign_resources_sorted(&head, realloc_head, fail_head);
  436. }
  437. void pci_setup_cardbus(struct pci_bus *bus)
  438. {
  439. struct pci_dev *bridge = bus->self;
  440. struct resource *res;
  441. struct pci_bus_region region;
  442. pci_info(bridge, "CardBus bridge to %pR\n",
  443. &bus->busn_res);
  444. res = bus->resource[0];
  445. pcibios_resource_to_bus(bridge->bus, &region, res);
  446. if (res->flags & IORESOURCE_IO) {
  447. /*
  448. * The IO resource is allocated a range twice as large as it
  449. * would normally need. This allows us to set both IO regs.
  450. */
  451. pci_info(bridge, " bridge window %pR\n", res);
  452. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  453. region.start);
  454. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  455. region.end);
  456. }
  457. res = bus->resource[1];
  458. pcibios_resource_to_bus(bridge->bus, &region, res);
  459. if (res->flags & IORESOURCE_IO) {
  460. pci_info(bridge, " bridge window %pR\n", res);
  461. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  462. region.start);
  463. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  464. region.end);
  465. }
  466. res = bus->resource[2];
  467. pcibios_resource_to_bus(bridge->bus, &region, res);
  468. if (res->flags & IORESOURCE_MEM) {
  469. pci_info(bridge, " bridge window %pR\n", res);
  470. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  471. region.start);
  472. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  473. region.end);
  474. }
  475. res = bus->resource[3];
  476. pcibios_resource_to_bus(bridge->bus, &region, res);
  477. if (res->flags & IORESOURCE_MEM) {
  478. pci_info(bridge, " bridge window %pR\n", res);
  479. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  480. region.start);
  481. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  482. region.end);
  483. }
  484. }
  485. EXPORT_SYMBOL(pci_setup_cardbus);
  486. /*
  487. * Initialize bridges with base/limit values we have collected. PCI-to-PCI
  488. * Bridge Architecture Specification rev. 1.1 (1998) requires that if there
  489. * are no I/O ports or memory behind the bridge, the corresponding range
  490. * must be turned off by writing base value greater than limit to the
  491. * bridge's base/limit registers.
  492. *
  493. * Note: care must be taken when updating I/O base/limit registers of
  494. * bridges which support 32-bit I/O. This update requires two config space
  495. * writes, so it's quite possible that an I/O window of the bridge will
  496. * have some undesirable address (e.g. 0) after the first write. Ditto
  497. * 64-bit prefetchable MMIO.
  498. */
  499. static void pci_setup_bridge_io(struct pci_dev *bridge)
  500. {
  501. struct resource *res;
  502. const char *res_name;
  503. struct pci_bus_region region;
  504. unsigned long io_mask;
  505. u8 io_base_lo, io_limit_lo;
  506. u16 l;
  507. u32 io_upper16;
  508. io_mask = PCI_IO_RANGE_MASK;
  509. if (bridge->io_window_1k)
  510. io_mask = PCI_IO_1K_RANGE_MASK;
  511. /* Set up the top and bottom of the PCI I/O segment for this bus */
  512. res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
  513. res_name = pci_resource_name(bridge, PCI_BRIDGE_IO_WINDOW);
  514. pcibios_resource_to_bus(bridge->bus, &region, res);
  515. if (res->flags & IORESOURCE_IO) {
  516. pci_read_config_word(bridge, PCI_IO_BASE, &l);
  517. io_base_lo = (region.start >> 8) & io_mask;
  518. io_limit_lo = (region.end >> 8) & io_mask;
  519. l = ((u16) io_limit_lo << 8) | io_base_lo;
  520. /* Set up upper 16 bits of I/O base/limit */
  521. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  522. pci_info(bridge, " %s %pR\n", res_name, res);
  523. } else {
  524. /* Clear upper 16 bits of I/O base/limit */
  525. io_upper16 = 0;
  526. l = 0x00f0;
  527. }
  528. /* Temporarily disable the I/O range before updating PCI_IO_BASE */
  529. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  530. /* Update lower 16 bits of I/O base/limit */
  531. pci_write_config_word(bridge, PCI_IO_BASE, l);
  532. /* Update upper 16 bits of I/O base/limit */
  533. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  534. }
  535. static void pci_setup_bridge_mmio(struct pci_dev *bridge)
  536. {
  537. struct resource *res;
  538. const char *res_name;
  539. struct pci_bus_region region;
  540. u32 l;
  541. /* Set up the top and bottom of the PCI Memory segment for this bus */
  542. res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
  543. res_name = pci_resource_name(bridge, PCI_BRIDGE_MEM_WINDOW);
  544. pcibios_resource_to_bus(bridge->bus, &region, res);
  545. if (res->flags & IORESOURCE_MEM) {
  546. l = (region.start >> 16) & 0xfff0;
  547. l |= region.end & 0xfff00000;
  548. pci_info(bridge, " %s %pR\n", res_name, res);
  549. } else {
  550. l = 0x0000fff0;
  551. }
  552. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  553. }
  554. static void pci_setup_bridge_mmio_pref(struct pci_dev *bridge)
  555. {
  556. struct resource *res;
  557. const char *res_name;
  558. struct pci_bus_region region;
  559. u32 l, bu, lu;
  560. /*
  561. * Clear out the upper 32 bits of PREF limit. If
  562. * PCI_PREF_BASE_UPPER32 was non-zero, this temporarily disables
  563. * PREF range, which is ok.
  564. */
  565. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  566. /* Set up PREF base/limit */
  567. bu = lu = 0;
  568. res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  569. res_name = pci_resource_name(bridge, PCI_BRIDGE_PREF_MEM_WINDOW);
  570. pcibios_resource_to_bus(bridge->bus, &region, res);
  571. if (res->flags & IORESOURCE_PREFETCH) {
  572. l = (region.start >> 16) & 0xfff0;
  573. l |= region.end & 0xfff00000;
  574. if (res->flags & IORESOURCE_MEM_64) {
  575. bu = upper_32_bits(region.start);
  576. lu = upper_32_bits(region.end);
  577. }
  578. pci_info(bridge, " %s %pR\n", res_name, res);
  579. } else {
  580. l = 0x0000fff0;
  581. }
  582. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  583. /* Set the upper 32 bits of PREF base & limit */
  584. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
  585. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
  586. }
  587. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  588. {
  589. struct pci_dev *bridge = bus->self;
  590. pci_info(bridge, "PCI bridge to %pR\n",
  591. &bus->busn_res);
  592. if (type & IORESOURCE_IO)
  593. pci_setup_bridge_io(bridge);
  594. if (type & IORESOURCE_MEM)
  595. pci_setup_bridge_mmio(bridge);
  596. if (type & IORESOURCE_PREFETCH)
  597. pci_setup_bridge_mmio_pref(bridge);
  598. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  599. }
  600. void __weak pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
  601. {
  602. }
  603. void pci_setup_bridge(struct pci_bus *bus)
  604. {
  605. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  606. IORESOURCE_PREFETCH;
  607. pcibios_setup_bridge(bus, type);
  608. __pci_setup_bridge(bus, type);
  609. }
  610. int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
  611. {
  612. if (i < PCI_BRIDGE_RESOURCES || i > PCI_BRIDGE_RESOURCE_END)
  613. return 0;
  614. if (pci_claim_resource(bridge, i) == 0)
  615. return 0; /* Claimed the window */
  616. if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  617. return 0;
  618. if (!pci_bus_clip_resource(bridge, i))
  619. return -EINVAL; /* Clipping didn't change anything */
  620. switch (i) {
  621. case PCI_BRIDGE_IO_WINDOW:
  622. pci_setup_bridge_io(bridge);
  623. break;
  624. case PCI_BRIDGE_MEM_WINDOW:
  625. pci_setup_bridge_mmio(bridge);
  626. break;
  627. case PCI_BRIDGE_PREF_MEM_WINDOW:
  628. pci_setup_bridge_mmio_pref(bridge);
  629. break;
  630. default:
  631. return -EINVAL;
  632. }
  633. if (pci_claim_resource(bridge, i) == 0)
  634. return 0; /* Claimed a smaller window */
  635. return -EINVAL;
  636. }
  637. /*
  638. * Check whether the bridge supports optional I/O and prefetchable memory
  639. * ranges. If not, the respective base/limit registers must be read-only
  640. * and read as 0.
  641. */
  642. static void pci_bridge_check_ranges(struct pci_bus *bus)
  643. {
  644. struct pci_dev *bridge = bus->self;
  645. struct resource *b_res;
  646. b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
  647. b_res->flags |= IORESOURCE_MEM;
  648. if (bridge->io_window) {
  649. b_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
  650. b_res->flags |= IORESOURCE_IO;
  651. }
  652. if (bridge->pref_window) {
  653. b_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  654. b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  655. if (bridge->pref_64_window) {
  656. b_res->flags |= IORESOURCE_MEM_64 |
  657. PCI_PREF_RANGE_TYPE_64;
  658. }
  659. }
  660. }
  661. /*
  662. * Helper function for sizing routines. Assigned resources have non-NULL
  663. * parent resource.
  664. *
  665. * Return first unassigned resource of the correct type. If there is none,
  666. * return first assigned resource of the correct type. If none of the
  667. * above, return NULL.
  668. *
  669. * Returning an assigned resource of the correct type allows the caller to
  670. * distinguish between already assigned and no resource of the correct type.
  671. */
  672. static struct resource *find_bus_resource_of_type(struct pci_bus *bus,
  673. unsigned long type_mask,
  674. unsigned long type)
  675. {
  676. struct resource *r, *r_assigned = NULL;
  677. pci_bus_for_each_resource(bus, r) {
  678. if (r == &ioport_resource || r == &iomem_resource)
  679. continue;
  680. if (r && (r->flags & type_mask) == type && !r->parent)
  681. return r;
  682. if (r && (r->flags & type_mask) == type && !r_assigned)
  683. r_assigned = r;
  684. }
  685. return r_assigned;
  686. }
  687. static resource_size_t calculate_iosize(resource_size_t size,
  688. resource_size_t min_size,
  689. resource_size_t size1,
  690. resource_size_t add_size,
  691. resource_size_t children_add_size,
  692. resource_size_t old_size,
  693. resource_size_t align)
  694. {
  695. if (size < min_size)
  696. size = min_size;
  697. if (old_size == 1)
  698. old_size = 0;
  699. /*
  700. * To be fixed in 2.5: we should have sort of HAVE_ISA flag in the
  701. * struct pci_bus.
  702. */
  703. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  704. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  705. #endif
  706. size = size + size1;
  707. size = max(size, add_size) + children_add_size;
  708. return ALIGN(max(size, old_size), align);
  709. }
  710. static resource_size_t calculate_memsize(resource_size_t size,
  711. resource_size_t min_size,
  712. resource_size_t add_size,
  713. resource_size_t children_add_size,
  714. resource_size_t old_size,
  715. resource_size_t align)
  716. {
  717. if (size < min_size)
  718. size = min_size;
  719. if (old_size == 1)
  720. old_size = 0;
  721. size = max(size, add_size) + children_add_size;
  722. return ALIGN(max(size, old_size), align);
  723. }
  724. resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
  725. unsigned long type)
  726. {
  727. return 1;
  728. }
  729. #define PCI_P2P_DEFAULT_MEM_ALIGN 0x100000 /* 1MiB */
  730. #define PCI_P2P_DEFAULT_IO_ALIGN 0x1000 /* 4KiB */
  731. #define PCI_P2P_DEFAULT_IO_ALIGN_1K 0x400 /* 1KiB */
  732. static resource_size_t window_alignment(struct pci_bus *bus, unsigned long type)
  733. {
  734. resource_size_t align = 1, arch_align;
  735. if (type & IORESOURCE_MEM)
  736. align = PCI_P2P_DEFAULT_MEM_ALIGN;
  737. else if (type & IORESOURCE_IO) {
  738. /*
  739. * Per spec, I/O windows are 4K-aligned, but some bridges have
  740. * an extension to support 1K alignment.
  741. */
  742. if (bus->self && bus->self->io_window_1k)
  743. align = PCI_P2P_DEFAULT_IO_ALIGN_1K;
  744. else
  745. align = PCI_P2P_DEFAULT_IO_ALIGN;
  746. }
  747. arch_align = pcibios_window_alignment(bus, type);
  748. return max(align, arch_align);
  749. }
  750. /**
  751. * pbus_size_io() - Size the I/O window of a given bus
  752. *
  753. * @bus: The bus
  754. * @min_size: The minimum I/O window that must be allocated
  755. * @add_size: Additional optional I/O window
  756. * @realloc_head: Track the additional I/O window on this list
  757. *
  758. * Sizing the I/O windows of the PCI-PCI bridge is trivial, since these
  759. * windows have 1K or 4K granularity and the I/O ranges of non-bridge PCI
  760. * devices are limited to 256 bytes. We must be careful with the ISA
  761. * aliasing though.
  762. */
  763. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  764. resource_size_t add_size,
  765. struct list_head *realloc_head)
  766. {
  767. struct pci_dev *dev;
  768. struct resource *b_res = find_bus_resource_of_type(bus, IORESOURCE_IO,
  769. IORESOURCE_IO);
  770. resource_size_t size = 0, size0 = 0, size1 = 0;
  771. resource_size_t children_add_size = 0;
  772. resource_size_t min_align, align;
  773. if (!b_res)
  774. return;
  775. /* If resource is already assigned, nothing more to do */
  776. if (b_res->parent)
  777. return;
  778. min_align = window_alignment(bus, IORESOURCE_IO);
  779. list_for_each_entry(dev, &bus->devices, bus_list) {
  780. struct resource *r;
  781. pci_dev_for_each_resource(dev, r) {
  782. unsigned long r_size;
  783. if (r->parent || !(r->flags & IORESOURCE_IO))
  784. continue;
  785. r_size = resource_size(r);
  786. if (r_size < 0x400)
  787. /* Might be re-aligned for ISA */
  788. size += r_size;
  789. else
  790. size1 += r_size;
  791. align = pci_resource_alignment(dev, r);
  792. if (align > min_align)
  793. min_align = align;
  794. if (realloc_head)
  795. children_add_size += get_res_add_size(realloc_head, r);
  796. }
  797. }
  798. size0 = calculate_iosize(size, min_size, size1, 0, 0,
  799. resource_size(b_res), min_align);
  800. size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
  801. calculate_iosize(size, min_size, size1, add_size, children_add_size,
  802. resource_size(b_res), min_align);
  803. if (!size0 && !size1) {
  804. if (bus->self && (b_res->start || b_res->end))
  805. pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
  806. b_res, &bus->busn_res);
  807. b_res->flags = 0;
  808. return;
  809. }
  810. b_res->start = min_align;
  811. b_res->end = b_res->start + size0 - 1;
  812. b_res->flags |= IORESOURCE_STARTALIGN;
  813. if (bus->self && size1 > size0 && realloc_head) {
  814. add_to_list(realloc_head, bus->self, b_res, size1-size0,
  815. min_align);
  816. pci_info(bus->self, "bridge window %pR to %pR add_size %llx\n",
  817. b_res, &bus->busn_res,
  818. (unsigned long long) size1 - size0);
  819. }
  820. }
  821. static inline resource_size_t calculate_mem_align(resource_size_t *aligns,
  822. int max_order)
  823. {
  824. resource_size_t align = 0;
  825. resource_size_t min_align = 0;
  826. int order;
  827. for (order = 0; order <= max_order; order++) {
  828. resource_size_t align1 = 1;
  829. align1 <<= order + __ffs(SZ_1M);
  830. if (!align)
  831. min_align = align1;
  832. else if (ALIGN(align + min_align, min_align) < align1)
  833. min_align = align1 >> 1;
  834. align += aligns[order];
  835. }
  836. return min_align;
  837. }
  838. /**
  839. * pbus_upstream_space_available - Check no upstream resource limits allocation
  840. * @bus: The bus
  841. * @mask: Mask the resource flag, then compare it with type
  842. * @type: The type of resource from bridge
  843. * @size: The size required from the bridge window
  844. * @align: Required alignment for the resource
  845. *
  846. * Checks that @size can fit inside the upstream bridge resources that are
  847. * already assigned.
  848. *
  849. * Return: %true if enough space is available on all assigned upstream
  850. * resources.
  851. */
  852. static bool pbus_upstream_space_available(struct pci_bus *bus, unsigned long mask,
  853. unsigned long type, resource_size_t size,
  854. resource_size_t align)
  855. {
  856. struct resource_constraint constraint = {
  857. .max = RESOURCE_SIZE_MAX,
  858. .align = align,
  859. };
  860. struct pci_bus *downstream = bus;
  861. struct resource *r;
  862. while ((bus = bus->parent)) {
  863. if (pci_is_root_bus(bus))
  864. break;
  865. pci_bus_for_each_resource(bus, r) {
  866. if (!r || !r->parent || (r->flags & mask) != type)
  867. continue;
  868. if (resource_size(r) >= size) {
  869. struct resource gap = {};
  870. if (find_resource_space(r, &gap, size, &constraint) == 0) {
  871. gap.flags = type;
  872. pci_dbg(bus->self,
  873. "Assigned bridge window %pR to %pR free space at %pR\n",
  874. r, &bus->busn_res, &gap);
  875. return true;
  876. }
  877. }
  878. if (bus->self) {
  879. pci_info(bus->self,
  880. "Assigned bridge window %pR to %pR cannot fit 0x%llx required for %s bridging to %pR\n",
  881. r, &bus->busn_res,
  882. (unsigned long long)size,
  883. pci_name(downstream->self),
  884. &downstream->busn_res);
  885. }
  886. return false;
  887. }
  888. }
  889. return true;
  890. }
  891. /**
  892. * pbus_size_mem() - Size the memory window of a given bus
  893. *
  894. * @bus: The bus
  895. * @mask: Mask the resource flag, then compare it with type
  896. * @type: The type of free resource from bridge
  897. * @type2: Second match type
  898. * @type3: Third match type
  899. * @min_size: The minimum memory window that must be allocated
  900. * @add_size: Additional optional memory window
  901. * @realloc_head: Track the additional memory window on this list
  902. *
  903. * Calculate the size of the bus and minimal alignment which guarantees
  904. * that all child resources fit in this size.
  905. *
  906. * Return -ENOSPC if there's no available bus resource of the desired
  907. * type. Otherwise, set the bus resource start/end to indicate the
  908. * required size, add things to realloc_head (if supplied), and return 0.
  909. */
  910. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  911. unsigned long type, unsigned long type2,
  912. unsigned long type3, resource_size_t min_size,
  913. resource_size_t add_size,
  914. struct list_head *realloc_head)
  915. {
  916. struct pci_dev *dev;
  917. resource_size_t min_align, win_align, align, size, size0, size1;
  918. resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */
  919. int order, max_order;
  920. struct resource *b_res = find_bus_resource_of_type(bus,
  921. mask | IORESOURCE_PREFETCH, type);
  922. resource_size_t children_add_size = 0;
  923. resource_size_t children_add_align = 0;
  924. resource_size_t add_align = 0;
  925. if (!b_res)
  926. return -ENOSPC;
  927. /* If resource is already assigned, nothing more to do */
  928. if (b_res->parent)
  929. return 0;
  930. memset(aligns, 0, sizeof(aligns));
  931. max_order = 0;
  932. size = 0;
  933. list_for_each_entry(dev, &bus->devices, bus_list) {
  934. struct resource *r;
  935. int i;
  936. pci_dev_for_each_resource(dev, r, i) {
  937. const char *r_name = pci_resource_name(dev, i);
  938. resource_size_t r_size;
  939. if (r->parent || (r->flags & IORESOURCE_PCI_FIXED) ||
  940. ((r->flags & mask) != type &&
  941. (r->flags & mask) != type2 &&
  942. (r->flags & mask) != type3))
  943. continue;
  944. r_size = resource_size(r);
  945. #ifdef CONFIG_PCI_IOV
  946. /* Put SRIOV requested res to the optional list */
  947. if (realloc_head && i >= PCI_IOV_RESOURCES &&
  948. i <= PCI_IOV_RESOURCE_END) {
  949. add_align = max(pci_resource_alignment(dev, r), add_align);
  950. r->end = r->start - 1;
  951. add_to_list(realloc_head, dev, r, r_size, 0 /* Don't care */);
  952. children_add_size += r_size;
  953. continue;
  954. }
  955. #endif
  956. /*
  957. * aligns[0] is for 1MB (since bridge memory
  958. * windows are always at least 1MB aligned), so
  959. * keep "order" from being negative for smaller
  960. * resources.
  961. */
  962. align = pci_resource_alignment(dev, r);
  963. order = __ffs(align) - __ffs(SZ_1M);
  964. if (order < 0)
  965. order = 0;
  966. if (order >= ARRAY_SIZE(aligns)) {
  967. pci_warn(dev, "%s %pR: disabling; bad alignment %#llx\n",
  968. r_name, r, (unsigned long long) align);
  969. r->flags = 0;
  970. continue;
  971. }
  972. size += max(r_size, align);
  973. /*
  974. * Exclude ranges with size > align from calculation of
  975. * the alignment.
  976. */
  977. if (r_size <= align)
  978. aligns[order] += align;
  979. if (order > max_order)
  980. max_order = order;
  981. if (realloc_head) {
  982. children_add_size += get_res_add_size(realloc_head, r);
  983. children_add_align = get_res_add_align(realloc_head, r);
  984. add_align = max(add_align, children_add_align);
  985. }
  986. }
  987. }
  988. win_align = window_alignment(bus, b_res->flags);
  989. min_align = calculate_mem_align(aligns, max_order);
  990. min_align = max(min_align, win_align);
  991. size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align);
  992. add_align = max(min_align, add_align);
  993. if (bus->self && size0 &&
  994. !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type,
  995. size0, add_align)) {
  996. min_align = 1ULL << (max_order + __ffs(SZ_1M));
  997. min_align = max(min_align, win_align);
  998. size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), win_align);
  999. pci_info(bus->self, "bridge window %pR to %pR requires relaxed alignment rules\n",
  1000. b_res, &bus->busn_res);
  1001. }
  1002. size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :
  1003. calculate_memsize(size, min_size, add_size, children_add_size,
  1004. resource_size(b_res), add_align);
  1005. if (!size0 && !size1) {
  1006. if (bus->self && (b_res->start || b_res->end))
  1007. pci_info(bus->self, "disabling bridge window %pR to %pR (unused)\n",
  1008. b_res, &bus->busn_res);
  1009. b_res->flags = 0;
  1010. return 0;
  1011. }
  1012. b_res->start = min_align;
  1013. b_res->end = size0 + min_align - 1;
  1014. b_res->flags |= IORESOURCE_STARTALIGN;
  1015. if (bus->self && size1 > size0 && realloc_head) {
  1016. add_to_list(realloc_head, bus->self, b_res, size1-size0, add_align);
  1017. pci_info(bus->self, "bridge window %pR to %pR add_size %llx add_align %llx\n",
  1018. b_res, &bus->busn_res,
  1019. (unsigned long long) (size1 - size0),
  1020. (unsigned long long) add_align);
  1021. }
  1022. return 0;
  1023. }
  1024. unsigned long pci_cardbus_resource_alignment(struct resource *res)
  1025. {
  1026. if (res->flags & IORESOURCE_IO)
  1027. return pci_cardbus_io_size;
  1028. if (res->flags & IORESOURCE_MEM)
  1029. return pci_cardbus_mem_size;
  1030. return 0;
  1031. }
  1032. static void pci_bus_size_cardbus(struct pci_bus *bus,
  1033. struct list_head *realloc_head)
  1034. {
  1035. struct pci_dev *bridge = bus->self;
  1036. struct resource *b_res;
  1037. resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
  1038. u16 ctrl;
  1039. b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
  1040. if (b_res->parent)
  1041. goto handle_b_res_1;
  1042. /*
  1043. * Reserve some resources for CardBus. We reserve a fixed amount
  1044. * of bus space for CardBus bridges.
  1045. */
  1046. b_res->start = pci_cardbus_io_size;
  1047. b_res->end = b_res->start + pci_cardbus_io_size - 1;
  1048. b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  1049. if (realloc_head) {
  1050. b_res->end -= pci_cardbus_io_size;
  1051. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  1052. pci_cardbus_io_size);
  1053. }
  1054. handle_b_res_1:
  1055. b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW];
  1056. if (b_res->parent)
  1057. goto handle_b_res_2;
  1058. b_res->start = pci_cardbus_io_size;
  1059. b_res->end = b_res->start + pci_cardbus_io_size - 1;
  1060. b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
  1061. if (realloc_head) {
  1062. b_res->end -= pci_cardbus_io_size;
  1063. add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
  1064. pci_cardbus_io_size);
  1065. }
  1066. handle_b_res_2:
  1067. /* MEM1 must not be pref MMIO */
  1068. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1069. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
  1070. ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  1071. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1072. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1073. }
  1074. /* Check whether prefetchable memory is supported by this bridge. */
  1075. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1076. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  1077. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  1078. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  1079. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  1080. }
  1081. b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW];
  1082. if (b_res->parent)
  1083. goto handle_b_res_3;
  1084. /*
  1085. * If we have prefetchable memory support, allocate two regions.
  1086. * Otherwise, allocate one region of twice the size.
  1087. */
  1088. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  1089. b_res->start = pci_cardbus_mem_size;
  1090. b_res->end = b_res->start + pci_cardbus_mem_size - 1;
  1091. b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
  1092. IORESOURCE_STARTALIGN;
  1093. if (realloc_head) {
  1094. b_res->end -= pci_cardbus_mem_size;
  1095. add_to_list(realloc_head, bridge, b_res,
  1096. pci_cardbus_mem_size, pci_cardbus_mem_size);
  1097. }
  1098. /* Reduce that to half */
  1099. b_res_3_size = pci_cardbus_mem_size;
  1100. }
  1101. handle_b_res_3:
  1102. b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW];
  1103. if (b_res->parent)
  1104. goto handle_done;
  1105. b_res->start = pci_cardbus_mem_size;
  1106. b_res->end = b_res->start + b_res_3_size - 1;
  1107. b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
  1108. if (realloc_head) {
  1109. b_res->end -= b_res_3_size;
  1110. add_to_list(realloc_head, bridge, b_res, b_res_3_size,
  1111. pci_cardbus_mem_size);
  1112. }
  1113. handle_done:
  1114. ;
  1115. }
  1116. void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
  1117. {
  1118. struct pci_dev *dev;
  1119. unsigned long mask, prefmask, type2 = 0, type3 = 0;
  1120. resource_size_t additional_io_size = 0, additional_mmio_size = 0,
  1121. additional_mmio_pref_size = 0;
  1122. struct resource *pref;
  1123. struct pci_host_bridge *host;
  1124. int hdr_type, ret;
  1125. list_for_each_entry(dev, &bus->devices, bus_list) {
  1126. struct pci_bus *b = dev->subordinate;
  1127. if (!b)
  1128. continue;
  1129. switch (dev->hdr_type) {
  1130. case PCI_HEADER_TYPE_CARDBUS:
  1131. pci_bus_size_cardbus(b, realloc_head);
  1132. break;
  1133. case PCI_HEADER_TYPE_BRIDGE:
  1134. default:
  1135. __pci_bus_size_bridges(b, realloc_head);
  1136. break;
  1137. }
  1138. }
  1139. /* The root bus? */
  1140. if (pci_is_root_bus(bus)) {
  1141. host = to_pci_host_bridge(bus->bridge);
  1142. if (!host->size_windows)
  1143. return;
  1144. pci_bus_for_each_resource(bus, pref)
  1145. if (pref && (pref->flags & IORESOURCE_PREFETCH))
  1146. break;
  1147. hdr_type = -1; /* Intentionally invalid - not a PCI device. */
  1148. } else {
  1149. pref = &bus->self->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  1150. hdr_type = bus->self->hdr_type;
  1151. }
  1152. switch (hdr_type) {
  1153. case PCI_HEADER_TYPE_CARDBUS:
  1154. /* Don't size CardBuses yet */
  1155. break;
  1156. case PCI_HEADER_TYPE_BRIDGE:
  1157. pci_bridge_check_ranges(bus);
  1158. if (bus->self->is_hotplug_bridge) {
  1159. additional_io_size = pci_hotplug_io_size;
  1160. additional_mmio_size = pci_hotplug_mmio_size;
  1161. additional_mmio_pref_size = pci_hotplug_mmio_pref_size;
  1162. }
  1163. fallthrough;
  1164. default:
  1165. pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
  1166. additional_io_size, realloc_head);
  1167. /*
  1168. * If there's a 64-bit prefetchable MMIO window, compute
  1169. * the size required to put all 64-bit prefetchable
  1170. * resources in it.
  1171. */
  1172. mask = IORESOURCE_MEM;
  1173. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  1174. if (pref && (pref->flags & IORESOURCE_MEM_64)) {
  1175. prefmask |= IORESOURCE_MEM_64;
  1176. ret = pbus_size_mem(bus, prefmask, prefmask,
  1177. prefmask, prefmask,
  1178. realloc_head ? 0 : additional_mmio_pref_size,
  1179. additional_mmio_pref_size, realloc_head);
  1180. /*
  1181. * If successful, all non-prefetchable resources
  1182. * and any 32-bit prefetchable resources will go in
  1183. * the non-prefetchable window.
  1184. */
  1185. if (ret == 0) {
  1186. mask = prefmask;
  1187. type2 = prefmask & ~IORESOURCE_MEM_64;
  1188. type3 = prefmask & ~IORESOURCE_PREFETCH;
  1189. }
  1190. }
  1191. /*
  1192. * If there is no 64-bit prefetchable window, compute the
  1193. * size required to put all prefetchable resources in the
  1194. * 32-bit prefetchable window (if there is one).
  1195. */
  1196. if (!type2) {
  1197. prefmask &= ~IORESOURCE_MEM_64;
  1198. ret = pbus_size_mem(bus, prefmask, prefmask,
  1199. prefmask, prefmask,
  1200. realloc_head ? 0 : additional_mmio_pref_size,
  1201. additional_mmio_pref_size, realloc_head);
  1202. /*
  1203. * If successful, only non-prefetchable resources
  1204. * will go in the non-prefetchable window.
  1205. */
  1206. if (ret == 0)
  1207. mask = prefmask;
  1208. else
  1209. additional_mmio_size += additional_mmio_pref_size;
  1210. type2 = type3 = IORESOURCE_MEM;
  1211. }
  1212. /*
  1213. * Compute the size required to put everything else in the
  1214. * non-prefetchable window. This includes:
  1215. *
  1216. * - all non-prefetchable resources
  1217. * - 32-bit prefetchable resources if there's a 64-bit
  1218. * prefetchable window or no prefetchable window at all
  1219. * - 64-bit prefetchable resources if there's no prefetchable
  1220. * window at all
  1221. *
  1222. * Note that the strategy in __pci_assign_resource() must match
  1223. * that used here. Specifically, we cannot put a 32-bit
  1224. * prefetchable resource in a 64-bit prefetchable window.
  1225. */
  1226. pbus_size_mem(bus, mask, IORESOURCE_MEM, type2, type3,
  1227. realloc_head ? 0 : additional_mmio_size,
  1228. additional_mmio_size, realloc_head);
  1229. break;
  1230. }
  1231. }
  1232. void pci_bus_size_bridges(struct pci_bus *bus)
  1233. {
  1234. __pci_bus_size_bridges(bus, NULL);
  1235. }
  1236. EXPORT_SYMBOL(pci_bus_size_bridges);
  1237. static void assign_fixed_resource_on_bus(struct pci_bus *b, struct resource *r)
  1238. {
  1239. struct resource *parent_r;
  1240. unsigned long mask = IORESOURCE_IO | IORESOURCE_MEM |
  1241. IORESOURCE_PREFETCH;
  1242. pci_bus_for_each_resource(b, parent_r) {
  1243. if (!parent_r)
  1244. continue;
  1245. if ((r->flags & mask) == (parent_r->flags & mask) &&
  1246. resource_contains(parent_r, r))
  1247. request_resource(parent_r, r);
  1248. }
  1249. }
  1250. /*
  1251. * Try to assign any resources marked as IORESOURCE_PCI_FIXED, as they are
  1252. * skipped by pbus_assign_resources_sorted().
  1253. */
  1254. static void pdev_assign_fixed_resources(struct pci_dev *dev)
  1255. {
  1256. struct resource *r;
  1257. pci_dev_for_each_resource(dev, r) {
  1258. struct pci_bus *b;
  1259. if (r->parent || !(r->flags & IORESOURCE_PCI_FIXED) ||
  1260. !(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  1261. continue;
  1262. b = dev->bus;
  1263. while (b && !r->parent) {
  1264. assign_fixed_resource_on_bus(b, r);
  1265. b = b->parent;
  1266. }
  1267. }
  1268. }
  1269. void __pci_bus_assign_resources(const struct pci_bus *bus,
  1270. struct list_head *realloc_head,
  1271. struct list_head *fail_head)
  1272. {
  1273. struct pci_bus *b;
  1274. struct pci_dev *dev;
  1275. pbus_assign_resources_sorted(bus, realloc_head, fail_head);
  1276. list_for_each_entry(dev, &bus->devices, bus_list) {
  1277. pdev_assign_fixed_resources(dev);
  1278. b = dev->subordinate;
  1279. if (!b)
  1280. continue;
  1281. __pci_bus_assign_resources(b, realloc_head, fail_head);
  1282. switch (dev->hdr_type) {
  1283. case PCI_HEADER_TYPE_BRIDGE:
  1284. if (!pci_is_enabled(dev))
  1285. pci_setup_bridge(b);
  1286. break;
  1287. case PCI_HEADER_TYPE_CARDBUS:
  1288. pci_setup_cardbus(b);
  1289. break;
  1290. default:
  1291. pci_info(dev, "not setting up bridge for bus %04x:%02x\n",
  1292. pci_domain_nr(b), b->number);
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. void pci_bus_assign_resources(const struct pci_bus *bus)
  1298. {
  1299. __pci_bus_assign_resources(bus, NULL, NULL);
  1300. }
  1301. EXPORT_SYMBOL(pci_bus_assign_resources);
  1302. static void pci_claim_device_resources(struct pci_dev *dev)
  1303. {
  1304. int i;
  1305. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  1306. struct resource *r = &dev->resource[i];
  1307. if (!r->flags || r->parent)
  1308. continue;
  1309. pci_claim_resource(dev, i);
  1310. }
  1311. }
  1312. static void pci_claim_bridge_resources(struct pci_dev *dev)
  1313. {
  1314. int i;
  1315. for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
  1316. struct resource *r = &dev->resource[i];
  1317. if (!r->flags || r->parent)
  1318. continue;
  1319. pci_claim_bridge_resource(dev, i);
  1320. }
  1321. }
  1322. static void pci_bus_allocate_dev_resources(struct pci_bus *b)
  1323. {
  1324. struct pci_dev *dev;
  1325. struct pci_bus *child;
  1326. list_for_each_entry(dev, &b->devices, bus_list) {
  1327. pci_claim_device_resources(dev);
  1328. child = dev->subordinate;
  1329. if (child)
  1330. pci_bus_allocate_dev_resources(child);
  1331. }
  1332. }
  1333. static void pci_bus_allocate_resources(struct pci_bus *b)
  1334. {
  1335. struct pci_bus *child;
  1336. /*
  1337. * Carry out a depth-first search on the PCI bus tree to allocate
  1338. * bridge apertures. Read the programmed bridge bases and
  1339. * recursively claim the respective bridge resources.
  1340. */
  1341. if (b->self) {
  1342. pci_read_bridge_bases(b);
  1343. pci_claim_bridge_resources(b->self);
  1344. }
  1345. list_for_each_entry(child, &b->children, node)
  1346. pci_bus_allocate_resources(child);
  1347. }
  1348. void pci_bus_claim_resources(struct pci_bus *b)
  1349. {
  1350. pci_bus_allocate_resources(b);
  1351. pci_bus_allocate_dev_resources(b);
  1352. }
  1353. EXPORT_SYMBOL(pci_bus_claim_resources);
  1354. static void __pci_bridge_assign_resources(const struct pci_dev *bridge,
  1355. struct list_head *add_head,
  1356. struct list_head *fail_head)
  1357. {
  1358. struct pci_bus *b;
  1359. pdev_assign_resources_sorted((struct pci_dev *)bridge,
  1360. add_head, fail_head);
  1361. b = bridge->subordinate;
  1362. if (!b)
  1363. return;
  1364. __pci_bus_assign_resources(b, add_head, fail_head);
  1365. switch (bridge->class >> 8) {
  1366. case PCI_CLASS_BRIDGE_PCI:
  1367. pci_setup_bridge(b);
  1368. break;
  1369. case PCI_CLASS_BRIDGE_CARDBUS:
  1370. pci_setup_cardbus(b);
  1371. break;
  1372. default:
  1373. pci_info(bridge, "not setting up bridge for bus %04x:%02x\n",
  1374. pci_domain_nr(b), b->number);
  1375. break;
  1376. }
  1377. }
  1378. #define PCI_RES_TYPE_MASK \
  1379. (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH |\
  1380. IORESOURCE_MEM_64)
  1381. static void pci_bridge_release_resources(struct pci_bus *bus,
  1382. unsigned long type)
  1383. {
  1384. struct pci_dev *dev = bus->self;
  1385. struct resource *r;
  1386. unsigned int old_flags;
  1387. struct resource *b_res;
  1388. int idx = 1;
  1389. b_res = &dev->resource[PCI_BRIDGE_RESOURCES];
  1390. /*
  1391. * 1. If IO port assignment fails, release bridge IO port.
  1392. * 2. If non pref MMIO assignment fails, release bridge nonpref MMIO.
  1393. * 3. If 64bit pref MMIO assignment fails, and bridge pref is 64bit,
  1394. * release bridge pref MMIO.
  1395. * 4. If pref MMIO assignment fails, and bridge pref is 32bit,
  1396. * release bridge pref MMIO.
  1397. * 5. If pref MMIO assignment fails, and bridge pref is not
  1398. * assigned, release bridge nonpref MMIO.
  1399. */
  1400. if (type & IORESOURCE_IO)
  1401. idx = 0;
  1402. else if (!(type & IORESOURCE_PREFETCH))
  1403. idx = 1;
  1404. else if ((type & IORESOURCE_MEM_64) &&
  1405. (b_res[2].flags & IORESOURCE_MEM_64))
  1406. idx = 2;
  1407. else if (!(b_res[2].flags & IORESOURCE_MEM_64) &&
  1408. (b_res[2].flags & IORESOURCE_PREFETCH))
  1409. idx = 2;
  1410. else
  1411. idx = 1;
  1412. r = &b_res[idx];
  1413. if (!r->parent)
  1414. return;
  1415. /* If there are children, release them all */
  1416. release_child_resources(r);
  1417. if (!release_resource(r)) {
  1418. type = old_flags = r->flags & PCI_RES_TYPE_MASK;
  1419. pci_info(dev, "resource %d %pR released\n",
  1420. PCI_BRIDGE_RESOURCES + idx, r);
  1421. /* Keep the old size */
  1422. r->end = resource_size(r) - 1;
  1423. r->start = 0;
  1424. r->flags = 0;
  1425. /* Avoiding touch the one without PREF */
  1426. if (type & IORESOURCE_PREFETCH)
  1427. type = IORESOURCE_PREFETCH;
  1428. __pci_setup_bridge(bus, type);
  1429. /* For next child res under same bridge */
  1430. r->flags = old_flags;
  1431. }
  1432. }
  1433. enum release_type {
  1434. leaf_only,
  1435. whole_subtree,
  1436. };
  1437. /*
  1438. * Try to release PCI bridge resources from leaf bridge, so we can allocate
  1439. * a larger window later.
  1440. */
  1441. static void pci_bus_release_bridge_resources(struct pci_bus *bus,
  1442. unsigned long type,
  1443. enum release_type rel_type)
  1444. {
  1445. struct pci_dev *dev;
  1446. bool is_leaf_bridge = true;
  1447. list_for_each_entry(dev, &bus->devices, bus_list) {
  1448. struct pci_bus *b = dev->subordinate;
  1449. if (!b)
  1450. continue;
  1451. is_leaf_bridge = false;
  1452. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1453. continue;
  1454. if (rel_type == whole_subtree)
  1455. pci_bus_release_bridge_resources(b, type,
  1456. whole_subtree);
  1457. }
  1458. if (pci_is_root_bus(bus))
  1459. return;
  1460. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1461. return;
  1462. if ((rel_type == whole_subtree) || is_leaf_bridge)
  1463. pci_bridge_release_resources(bus, type);
  1464. }
  1465. static void pci_bus_dump_res(struct pci_bus *bus)
  1466. {
  1467. struct resource *res;
  1468. int i;
  1469. pci_bus_for_each_resource(bus, res, i) {
  1470. if (!res || !res->end || !res->flags)
  1471. continue;
  1472. dev_info(&bus->dev, "resource %d %pR\n", i, res);
  1473. }
  1474. }
  1475. static void pci_bus_dump_resources(struct pci_bus *bus)
  1476. {
  1477. struct pci_bus *b;
  1478. struct pci_dev *dev;
  1479. pci_bus_dump_res(bus);
  1480. list_for_each_entry(dev, &bus->devices, bus_list) {
  1481. b = dev->subordinate;
  1482. if (!b)
  1483. continue;
  1484. pci_bus_dump_resources(b);
  1485. }
  1486. }
  1487. static int pci_bus_get_depth(struct pci_bus *bus)
  1488. {
  1489. int depth = 0;
  1490. struct pci_bus *child_bus;
  1491. list_for_each_entry(child_bus, &bus->children, node) {
  1492. int ret;
  1493. ret = pci_bus_get_depth(child_bus);
  1494. if (ret + 1 > depth)
  1495. depth = ret + 1;
  1496. }
  1497. return depth;
  1498. }
  1499. /*
  1500. * -1: undefined, will auto detect later
  1501. * 0: disabled by user
  1502. * 1: disabled by auto detect
  1503. * 2: enabled by user
  1504. * 3: enabled by auto detect
  1505. */
  1506. enum enable_type {
  1507. undefined = -1,
  1508. user_disabled,
  1509. auto_disabled,
  1510. user_enabled,
  1511. auto_enabled,
  1512. };
  1513. static enum enable_type pci_realloc_enable = undefined;
  1514. void __init pci_realloc_get_opt(char *str)
  1515. {
  1516. if (!strncmp(str, "off", 3))
  1517. pci_realloc_enable = user_disabled;
  1518. else if (!strncmp(str, "on", 2))
  1519. pci_realloc_enable = user_enabled;
  1520. }
  1521. static bool pci_realloc_enabled(enum enable_type enable)
  1522. {
  1523. return enable >= user_enabled;
  1524. }
  1525. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
  1526. static int iov_resources_unassigned(struct pci_dev *dev, void *data)
  1527. {
  1528. int i;
  1529. bool *unassigned = data;
  1530. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  1531. struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
  1532. struct pci_bus_region region;
  1533. /* Not assigned or rejected by kernel? */
  1534. if (!r->flags)
  1535. continue;
  1536. pcibios_resource_to_bus(dev->bus, &region, r);
  1537. if (!region.start) {
  1538. *unassigned = true;
  1539. return 1; /* Return early from pci_walk_bus() */
  1540. }
  1541. }
  1542. return 0;
  1543. }
  1544. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1545. enum enable_type enable_local)
  1546. {
  1547. bool unassigned = false;
  1548. struct pci_host_bridge *host;
  1549. if (enable_local != undefined)
  1550. return enable_local;
  1551. host = pci_find_host_bridge(bus);
  1552. if (host->preserve_config)
  1553. return auto_disabled;
  1554. pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
  1555. if (unassigned)
  1556. return auto_enabled;
  1557. return enable_local;
  1558. }
  1559. #else
  1560. static enum enable_type pci_realloc_detect(struct pci_bus *bus,
  1561. enum enable_type enable_local)
  1562. {
  1563. return enable_local;
  1564. }
  1565. #endif
  1566. static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
  1567. struct list_head *add_list,
  1568. resource_size_t new_size)
  1569. {
  1570. resource_size_t add_size, size = resource_size(res);
  1571. if (res->parent)
  1572. return;
  1573. if (!new_size)
  1574. return;
  1575. if (new_size > size) {
  1576. add_size = new_size - size;
  1577. pci_dbg(bridge, "bridge window %pR extended by %pa\n", res,
  1578. &add_size);
  1579. } else if (new_size < size) {
  1580. add_size = size - new_size;
  1581. pci_dbg(bridge, "bridge window %pR shrunken by %pa\n", res,
  1582. &add_size);
  1583. } else {
  1584. return;
  1585. }
  1586. res->end = res->start + new_size - 1;
  1587. /* If the resource is part of the add_list, remove it now */
  1588. if (add_list)
  1589. remove_from_list(add_list, res);
  1590. }
  1591. static void remove_dev_resource(struct resource *avail, struct pci_dev *dev,
  1592. struct resource *res)
  1593. {
  1594. resource_size_t size, align, tmp;
  1595. size = resource_size(res);
  1596. if (!size)
  1597. return;
  1598. align = pci_resource_alignment(dev, res);
  1599. align = align ? ALIGN(avail->start, align) - avail->start : 0;
  1600. tmp = align + size;
  1601. avail->start = min(avail->start + tmp, avail->end + 1);
  1602. }
  1603. static void remove_dev_resources(struct pci_dev *dev, struct resource *io,
  1604. struct resource *mmio,
  1605. struct resource *mmio_pref)
  1606. {
  1607. struct resource *res;
  1608. pci_dev_for_each_resource(dev, res) {
  1609. if (resource_type(res) == IORESOURCE_IO) {
  1610. remove_dev_resource(io, dev, res);
  1611. } else if (resource_type(res) == IORESOURCE_MEM) {
  1612. /*
  1613. * Make sure prefetchable memory is reduced from
  1614. * the correct resource. Specifically we put 32-bit
  1615. * prefetchable memory in non-prefetchable window
  1616. * if there is an 64-bit prefetchable window.
  1617. *
  1618. * See comments in __pci_bus_size_bridges() for
  1619. * more information.
  1620. */
  1621. if ((res->flags & IORESOURCE_PREFETCH) &&
  1622. ((res->flags & IORESOURCE_MEM_64) ==
  1623. (mmio_pref->flags & IORESOURCE_MEM_64)))
  1624. remove_dev_resource(mmio_pref, dev, res);
  1625. else
  1626. remove_dev_resource(mmio, dev, res);
  1627. }
  1628. }
  1629. }
  1630. /*
  1631. * io, mmio and mmio_pref contain the total amount of bridge window space
  1632. * available. This includes the minimal space needed to cover all the
  1633. * existing devices on the bus and the possible extra space that can be
  1634. * shared with the bridges.
  1635. */
  1636. static void pci_bus_distribute_available_resources(struct pci_bus *bus,
  1637. struct list_head *add_list,
  1638. struct resource io,
  1639. struct resource mmio,
  1640. struct resource mmio_pref)
  1641. {
  1642. unsigned int normal_bridges = 0, hotplug_bridges = 0;
  1643. struct resource *io_res, *mmio_res, *mmio_pref_res;
  1644. struct pci_dev *dev, *bridge = bus->self;
  1645. resource_size_t io_per_b, mmio_per_b, mmio_pref_per_b, align;
  1646. io_res = &bridge->resource[PCI_BRIDGE_IO_WINDOW];
  1647. mmio_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
  1648. mmio_pref_res = &bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  1649. /*
  1650. * The alignment of this bridge is yet to be considered, hence it must
  1651. * be done now before extending its bridge window.
  1652. */
  1653. align = pci_resource_alignment(bridge, io_res);
  1654. if (!io_res->parent && align)
  1655. io.start = min(ALIGN(io.start, align), io.end + 1);
  1656. align = pci_resource_alignment(bridge, mmio_res);
  1657. if (!mmio_res->parent && align)
  1658. mmio.start = min(ALIGN(mmio.start, align), mmio.end + 1);
  1659. align = pci_resource_alignment(bridge, mmio_pref_res);
  1660. if (!mmio_pref_res->parent && align)
  1661. mmio_pref.start = min(ALIGN(mmio_pref.start, align),
  1662. mmio_pref.end + 1);
  1663. /*
  1664. * Now that we have adjusted for alignment, update the bridge window
  1665. * resources to fill as much remaining resource space as possible.
  1666. */
  1667. adjust_bridge_window(bridge, io_res, add_list, resource_size(&io));
  1668. adjust_bridge_window(bridge, mmio_res, add_list, resource_size(&mmio));
  1669. adjust_bridge_window(bridge, mmio_pref_res, add_list,
  1670. resource_size(&mmio_pref));
  1671. /*
  1672. * Calculate how many hotplug bridges and normal bridges there
  1673. * are on this bus. We will distribute the additional available
  1674. * resources between hotplug bridges.
  1675. */
  1676. for_each_pci_bridge(dev, bus) {
  1677. if (dev->is_hotplug_bridge)
  1678. hotplug_bridges++;
  1679. else
  1680. normal_bridges++;
  1681. }
  1682. if (!(hotplug_bridges + normal_bridges))
  1683. return;
  1684. /*
  1685. * Calculate the amount of space we can forward from "bus" to any
  1686. * downstream buses, i.e., the space left over after assigning the
  1687. * BARs and windows on "bus".
  1688. */
  1689. list_for_each_entry(dev, &bus->devices, bus_list) {
  1690. if (!dev->is_virtfn)
  1691. remove_dev_resources(dev, &io, &mmio, &mmio_pref);
  1692. }
  1693. /*
  1694. * If there is at least one hotplug bridge on this bus it gets all
  1695. * the extra resource space that was left after the reductions
  1696. * above.
  1697. *
  1698. * If there are no hotplug bridges the extra resource space is
  1699. * split between non-hotplug bridges. This is to allow possible
  1700. * hotplug bridges below them to get the extra space as well.
  1701. */
  1702. if (hotplug_bridges) {
  1703. io_per_b = div64_ul(resource_size(&io), hotplug_bridges);
  1704. mmio_per_b = div64_ul(resource_size(&mmio), hotplug_bridges);
  1705. mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
  1706. hotplug_bridges);
  1707. } else {
  1708. io_per_b = div64_ul(resource_size(&io), normal_bridges);
  1709. mmio_per_b = div64_ul(resource_size(&mmio), normal_bridges);
  1710. mmio_pref_per_b = div64_ul(resource_size(&mmio_pref),
  1711. normal_bridges);
  1712. }
  1713. for_each_pci_bridge(dev, bus) {
  1714. struct resource *res;
  1715. struct pci_bus *b;
  1716. b = dev->subordinate;
  1717. if (!b)
  1718. continue;
  1719. if (hotplug_bridges && !dev->is_hotplug_bridge)
  1720. continue;
  1721. res = &dev->resource[PCI_BRIDGE_IO_WINDOW];
  1722. /*
  1723. * Make sure the split resource space is properly aligned
  1724. * for bridge windows (align it down to avoid going above
  1725. * what is available).
  1726. */
  1727. align = pci_resource_alignment(dev, res);
  1728. io.end = align ? io.start + ALIGN_DOWN(io_per_b, align) - 1
  1729. : io.start + io_per_b - 1;
  1730. /*
  1731. * The x_per_b holds the extra resource space that can be
  1732. * added for each bridge but there is the minimal already
  1733. * reserved as well so adjust x.start down accordingly to
  1734. * cover the whole space.
  1735. */
  1736. io.start -= resource_size(res);
  1737. res = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
  1738. align = pci_resource_alignment(dev, res);
  1739. mmio.end = align ? mmio.start + ALIGN_DOWN(mmio_per_b, align) - 1
  1740. : mmio.start + mmio_per_b - 1;
  1741. mmio.start -= resource_size(res);
  1742. res = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  1743. align = pci_resource_alignment(dev, res);
  1744. mmio_pref.end = align ? mmio_pref.start +
  1745. ALIGN_DOWN(mmio_pref_per_b, align) - 1
  1746. : mmio_pref.start + mmio_pref_per_b - 1;
  1747. mmio_pref.start -= resource_size(res);
  1748. pci_bus_distribute_available_resources(b, add_list, io, mmio,
  1749. mmio_pref);
  1750. io.start += io.end + 1;
  1751. mmio.start += mmio.end + 1;
  1752. mmio_pref.start += mmio_pref.end + 1;
  1753. }
  1754. }
  1755. static void pci_bridge_distribute_available_resources(struct pci_dev *bridge,
  1756. struct list_head *add_list)
  1757. {
  1758. struct resource available_io, available_mmio, available_mmio_pref;
  1759. if (!bridge->is_hotplug_bridge)
  1760. return;
  1761. pci_dbg(bridge, "distributing available resources\n");
  1762. /* Take the initial extra resources from the hotplug port */
  1763. available_io = bridge->resource[PCI_BRIDGE_IO_WINDOW];
  1764. available_mmio = bridge->resource[PCI_BRIDGE_MEM_WINDOW];
  1765. available_mmio_pref = bridge->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  1766. pci_bus_distribute_available_resources(bridge->subordinate,
  1767. add_list, available_io,
  1768. available_mmio,
  1769. available_mmio_pref);
  1770. }
  1771. static bool pci_bridge_resources_not_assigned(struct pci_dev *dev)
  1772. {
  1773. const struct resource *r;
  1774. /*
  1775. * If the child device's resources are not yet assigned it means we
  1776. * are configuring them (not the boot firmware), so we should be
  1777. * able to extend the upstream bridge resources in the same way we
  1778. * do with the normal hotplug case.
  1779. */
  1780. r = &dev->resource[PCI_BRIDGE_IO_WINDOW];
  1781. if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
  1782. return false;
  1783. r = &dev->resource[PCI_BRIDGE_MEM_WINDOW];
  1784. if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
  1785. return false;
  1786. r = &dev->resource[PCI_BRIDGE_PREF_MEM_WINDOW];
  1787. if (r->flags && !(r->flags & IORESOURCE_STARTALIGN))
  1788. return false;
  1789. return true;
  1790. }
  1791. static void
  1792. pci_root_bus_distribute_available_resources(struct pci_bus *bus,
  1793. struct list_head *add_list)
  1794. {
  1795. struct pci_dev *dev, *bridge = bus->self;
  1796. for_each_pci_bridge(dev, bus) {
  1797. struct pci_bus *b;
  1798. b = dev->subordinate;
  1799. if (!b)
  1800. continue;
  1801. /*
  1802. * Need to check "bridge" here too because it is NULL
  1803. * in case of root bus.
  1804. */
  1805. if (bridge && pci_bridge_resources_not_assigned(dev))
  1806. pci_bridge_distribute_available_resources(dev, add_list);
  1807. else
  1808. pci_root_bus_distribute_available_resources(b, add_list);
  1809. }
  1810. }
  1811. /*
  1812. * First try will not touch PCI bridge res.
  1813. * Second and later try will clear small leaf bridge res.
  1814. * Will stop till to the max depth if can not find good one.
  1815. */
  1816. void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
  1817. {
  1818. LIST_HEAD(realloc_head);
  1819. /* List of resources that want additional resources */
  1820. struct list_head *add_list = NULL;
  1821. int tried_times = 0;
  1822. enum release_type rel_type = leaf_only;
  1823. LIST_HEAD(fail_head);
  1824. struct pci_dev_resource *fail_res;
  1825. int pci_try_num = 1;
  1826. enum enable_type enable_local;
  1827. /* Don't realloc if asked to do so */
  1828. enable_local = pci_realloc_detect(bus, pci_realloc_enable);
  1829. if (pci_realloc_enabled(enable_local)) {
  1830. int max_depth = pci_bus_get_depth(bus);
  1831. pci_try_num = max_depth + 1;
  1832. dev_info(&bus->dev, "max bus depth: %d pci_try_num: %d\n",
  1833. max_depth, pci_try_num);
  1834. }
  1835. again:
  1836. /*
  1837. * Last try will use add_list, otherwise will try good to have as must
  1838. * have, so can realloc parent bridge resource
  1839. */
  1840. if (tried_times + 1 == pci_try_num)
  1841. add_list = &realloc_head;
  1842. /*
  1843. * Depth first, calculate sizes and alignments of all subordinate buses.
  1844. */
  1845. __pci_bus_size_bridges(bus, add_list);
  1846. pci_root_bus_distribute_available_resources(bus, add_list);
  1847. /* Depth last, allocate resources and update the hardware. */
  1848. __pci_bus_assign_resources(bus, add_list, &fail_head);
  1849. if (add_list)
  1850. BUG_ON(!list_empty(add_list));
  1851. tried_times++;
  1852. /* Any device complain? */
  1853. if (list_empty(&fail_head))
  1854. goto dump;
  1855. if (tried_times >= pci_try_num) {
  1856. if (enable_local == undefined)
  1857. dev_info(&bus->dev, "Some PCI device resources are unassigned, try booting with pci=realloc\n");
  1858. else if (enable_local == auto_enabled)
  1859. dev_info(&bus->dev, "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
  1860. free_list(&fail_head);
  1861. goto dump;
  1862. }
  1863. dev_info(&bus->dev, "No. %d try to assign unassigned res\n",
  1864. tried_times + 1);
  1865. /* Third times and later will not check if it is leaf */
  1866. if ((tried_times + 1) > 2)
  1867. rel_type = whole_subtree;
  1868. /*
  1869. * Try to release leaf bridge's resources that doesn't fit resource of
  1870. * child device under that bridge.
  1871. */
  1872. list_for_each_entry(fail_res, &fail_head, list)
  1873. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1874. fail_res->flags & PCI_RES_TYPE_MASK,
  1875. rel_type);
  1876. /* Restore size and flags */
  1877. list_for_each_entry(fail_res, &fail_head, list) {
  1878. struct resource *res = fail_res->res;
  1879. int idx;
  1880. res->start = fail_res->start;
  1881. res->end = fail_res->end;
  1882. res->flags = fail_res->flags;
  1883. if (pci_is_bridge(fail_res->dev)) {
  1884. idx = res - &fail_res->dev->resource[0];
  1885. if (idx >= PCI_BRIDGE_RESOURCES &&
  1886. idx <= PCI_BRIDGE_RESOURCE_END)
  1887. res->flags = 0;
  1888. }
  1889. }
  1890. free_list(&fail_head);
  1891. goto again;
  1892. dump:
  1893. /* Dump the resource on buses */
  1894. pci_bus_dump_resources(bus);
  1895. }
  1896. void pci_assign_unassigned_resources(void)
  1897. {
  1898. struct pci_bus *root_bus;
  1899. list_for_each_entry(root_bus, &pci_root_buses, node) {
  1900. pci_assign_unassigned_root_bus_resources(root_bus);
  1901. /* Make sure the root bridge has a companion ACPI device */
  1902. if (ACPI_HANDLE(root_bus->bridge))
  1903. acpi_ioapic_add(ACPI_HANDLE(root_bus->bridge));
  1904. }
  1905. }
  1906. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  1907. {
  1908. struct pci_bus *parent = bridge->subordinate;
  1909. /* List of resources that want additional resources */
  1910. LIST_HEAD(add_list);
  1911. int tried_times = 0;
  1912. LIST_HEAD(fail_head);
  1913. struct pci_dev_resource *fail_res;
  1914. int retval;
  1915. again:
  1916. __pci_bus_size_bridges(parent, &add_list);
  1917. /*
  1918. * Distribute remaining resources (if any) equally between hotplug
  1919. * bridges below. This makes it possible to extend the hierarchy
  1920. * later without running out of resources.
  1921. */
  1922. pci_bridge_distribute_available_resources(bridge, &add_list);
  1923. __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
  1924. BUG_ON(!list_empty(&add_list));
  1925. tried_times++;
  1926. if (list_empty(&fail_head))
  1927. goto enable_all;
  1928. if (tried_times >= 2) {
  1929. /* Still fail, don't need to try more */
  1930. free_list(&fail_head);
  1931. goto enable_all;
  1932. }
  1933. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  1934. tried_times + 1);
  1935. /*
  1936. * Try to release leaf bridge's resources that aren't big enough
  1937. * to contain child device resources.
  1938. */
  1939. list_for_each_entry(fail_res, &fail_head, list)
  1940. pci_bus_release_bridge_resources(fail_res->dev->bus,
  1941. fail_res->flags & PCI_RES_TYPE_MASK,
  1942. whole_subtree);
  1943. /* Restore size and flags */
  1944. list_for_each_entry(fail_res, &fail_head, list) {
  1945. struct resource *res = fail_res->res;
  1946. int idx;
  1947. res->start = fail_res->start;
  1948. res->end = fail_res->end;
  1949. res->flags = fail_res->flags;
  1950. if (pci_is_bridge(fail_res->dev)) {
  1951. idx = res - &fail_res->dev->resource[0];
  1952. if (idx >= PCI_BRIDGE_RESOURCES &&
  1953. idx <= PCI_BRIDGE_RESOURCE_END)
  1954. res->flags = 0;
  1955. }
  1956. }
  1957. free_list(&fail_head);
  1958. goto again;
  1959. enable_all:
  1960. retval = pci_reenable_device(bridge);
  1961. if (retval)
  1962. pci_err(bridge, "Error reenabling bridge (%d)\n", retval);
  1963. pci_set_master(bridge);
  1964. }
  1965. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
  1966. int pci_reassign_bridge_resources(struct pci_dev *bridge, unsigned long type)
  1967. {
  1968. struct pci_dev_resource *dev_res;
  1969. struct pci_dev *next;
  1970. LIST_HEAD(saved);
  1971. LIST_HEAD(added);
  1972. LIST_HEAD(failed);
  1973. unsigned int i;
  1974. int ret;
  1975. down_read(&pci_bus_sem);
  1976. /* Walk to the root hub, releasing bridge BARs when possible */
  1977. next = bridge;
  1978. do {
  1979. bridge = next;
  1980. for (i = PCI_BRIDGE_RESOURCES; i < PCI_BRIDGE_RESOURCE_END;
  1981. i++) {
  1982. struct resource *res = &bridge->resource[i];
  1983. const char *res_name = pci_resource_name(bridge, i);
  1984. if ((res->flags ^ type) & PCI_RES_TYPE_MASK)
  1985. continue;
  1986. /* Ignore BARs which are still in use */
  1987. if (res->child)
  1988. continue;
  1989. ret = add_to_list(&saved, bridge, res, 0, 0);
  1990. if (ret)
  1991. goto cleanup;
  1992. pci_info(bridge, "%s %pR: releasing\n", res_name, res);
  1993. if (res->parent)
  1994. release_resource(res);
  1995. res->start = 0;
  1996. res->end = 0;
  1997. break;
  1998. }
  1999. if (i == PCI_BRIDGE_RESOURCE_END)
  2000. break;
  2001. next = bridge->bus ? bridge->bus->self : NULL;
  2002. } while (next);
  2003. if (list_empty(&saved)) {
  2004. up_read(&pci_bus_sem);
  2005. return -ENOENT;
  2006. }
  2007. __pci_bus_size_bridges(bridge->subordinate, &added);
  2008. __pci_bridge_assign_resources(bridge, &added, &failed);
  2009. BUG_ON(!list_empty(&added));
  2010. if (!list_empty(&failed)) {
  2011. ret = -ENOSPC;
  2012. goto cleanup;
  2013. }
  2014. list_for_each_entry(dev_res, &saved, list) {
  2015. /* Skip the bridge we just assigned resources for */
  2016. if (bridge == dev_res->dev)
  2017. continue;
  2018. bridge = dev_res->dev;
  2019. pci_setup_bridge(bridge->subordinate);
  2020. }
  2021. free_list(&saved);
  2022. up_read(&pci_bus_sem);
  2023. return 0;
  2024. cleanup:
  2025. /* Restore size and flags */
  2026. list_for_each_entry(dev_res, &failed, list) {
  2027. struct resource *res = dev_res->res;
  2028. res->start = dev_res->start;
  2029. res->end = dev_res->end;
  2030. res->flags = dev_res->flags;
  2031. }
  2032. free_list(&failed);
  2033. /* Revert to the old configuration */
  2034. list_for_each_entry(dev_res, &saved, list) {
  2035. struct resource *res = dev_res->res;
  2036. bridge = dev_res->dev;
  2037. i = res - bridge->resource;
  2038. res->start = dev_res->start;
  2039. res->end = dev_res->end;
  2040. res->flags = dev_res->flags;
  2041. pci_claim_resource(bridge, i);
  2042. pci_setup_bridge(bridge->subordinate);
  2043. }
  2044. free_list(&saved);
  2045. up_read(&pci_bus_sem);
  2046. return ret;
  2047. }
  2048. void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
  2049. {
  2050. struct pci_dev *dev;
  2051. /* List of resources that want additional resources */
  2052. LIST_HEAD(add_list);
  2053. down_read(&pci_bus_sem);
  2054. for_each_pci_bridge(dev, bus)
  2055. if (pci_has_subordinate(dev))
  2056. __pci_bus_size_bridges(dev->subordinate, &add_list);
  2057. up_read(&pci_bus_sem);
  2058. __pci_bus_assign_resources(bus, &add_list, NULL);
  2059. BUG_ON(!list_empty(&add_list));
  2060. }
  2061. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);