setup-bus.c 62 KB

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