setup-bus.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * drivers/pci/setup-bus.c
  3. *
  4. * Extruded from code written by
  5. * Dave Rusling (david.rusling@reo.mts.dec.com)
  6. * David Mosberger (davidm@cs.arizona.edu)
  7. * David Miller (davem@redhat.com)
  8. *
  9. * Support routines for initializing a PCI subsystem.
  10. */
  11. /*
  12. * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  13. * PCI-PCI bridges cleanup, sorted resource allocation.
  14. * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  15. * Converted to allocation in 3 passes, which gives
  16. * tighter packing. Prefetchable range support.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/cache.h>
  25. #include <linux/slab.h>
  26. #include "pci.h"
  27. struct resource_list_x {
  28. struct resource_list_x *next;
  29. struct resource *res;
  30. struct pci_dev *dev;
  31. resource_size_t start;
  32. resource_size_t end;
  33. resource_size_t add_size;
  34. unsigned long flags;
  35. };
  36. #define free_list(type, head) do { \
  37. struct type *list, *tmp; \
  38. for (list = (head)->next; list;) { \
  39. tmp = list; \
  40. list = list->next; \
  41. kfree(tmp); \
  42. } \
  43. (head)->next = NULL; \
  44. } while (0)
  45. /**
  46. * add_to_list() - add a new resource tracker to the list
  47. * @head: Head of the list
  48. * @dev: device corresponding to which the resource
  49. * belongs
  50. * @res: The resource to be tracked
  51. * @add_size: additional size to be optionally added
  52. * to the resource
  53. */
  54. static void add_to_list(struct resource_list_x *head,
  55. struct pci_dev *dev, struct resource *res,
  56. resource_size_t add_size)
  57. {
  58. struct resource_list_x *list = head;
  59. struct resource_list_x *ln = list->next;
  60. struct resource_list_x *tmp;
  61. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  62. if (!tmp) {
  63. pr_warning("add_to_list: kmalloc() failed!\n");
  64. return;
  65. }
  66. tmp->next = ln;
  67. tmp->res = res;
  68. tmp->dev = dev;
  69. tmp->start = res->start;
  70. tmp->end = res->end;
  71. tmp->flags = res->flags;
  72. tmp->add_size = add_size;
  73. list->next = tmp;
  74. }
  75. static void add_to_failed_list(struct resource_list_x *head,
  76. struct pci_dev *dev, struct resource *res)
  77. {
  78. add_to_list(head, dev, res, 0);
  79. }
  80. static void __dev_sort_resources(struct pci_dev *dev,
  81. struct resource_list *head)
  82. {
  83. u16 class = dev->class >> 8;
  84. /* Don't touch classless devices or host bridges or ioapics. */
  85. if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
  86. return;
  87. /* Don't touch ioapic devices already enabled by firmware */
  88. if (class == PCI_CLASS_SYSTEM_PIC) {
  89. u16 command;
  90. pci_read_config_word(dev, PCI_COMMAND, &command);
  91. if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  92. return;
  93. }
  94. pdev_sort_resources(dev, head);
  95. }
  96. static inline void reset_resource(struct resource *res)
  97. {
  98. res->start = 0;
  99. res->end = 0;
  100. res->flags = 0;
  101. }
  102. /**
  103. * adjust_resources_sorted() - satisfy any additional resource requests
  104. *
  105. * @add_head : head of the list tracking requests requiring additional
  106. * resources
  107. * @head : head of the list tracking requests with allocated
  108. * resources
  109. *
  110. * Walk through each element of the add_head and try to procure
  111. * additional resources for the element, provided the element
  112. * is in the head list.
  113. */
  114. static void adjust_resources_sorted(struct resource_list_x *add_head,
  115. struct resource_list *head)
  116. {
  117. struct resource *res;
  118. struct resource_list_x *list, *tmp, *prev;
  119. struct resource_list *hlist;
  120. resource_size_t add_size;
  121. int idx;
  122. prev = add_head;
  123. for (list = add_head->next; list;) {
  124. res = list->res;
  125. /* skip resource that has been reset */
  126. if (!res->flags)
  127. goto out;
  128. /* skip this resource if not found in head list */
  129. for (hlist = head->next; hlist && hlist->res != res;
  130. hlist = hlist->next);
  131. if (!hlist) { /* just skip */
  132. prev = list;
  133. list = list->next;
  134. continue;
  135. }
  136. idx = res - &list->dev->resource[0];
  137. add_size=list->add_size;
  138. if (!resource_size(res) && add_size) {
  139. res->end = res->start + add_size - 1;
  140. if(pci_assign_resource(list->dev, idx))
  141. reset_resource(res);
  142. } else if (add_size) {
  143. adjust_resource(res, res->start,
  144. resource_size(res) + add_size);
  145. }
  146. out:
  147. tmp = list;
  148. prev->next = list = list->next;
  149. kfree(tmp);
  150. }
  151. }
  152. /**
  153. * assign_requested_resources_sorted() - satisfy resource requests
  154. *
  155. * @head : head of the list tracking requests for resources
  156. * @failed_list : head of the list tracking requests that could
  157. * not be allocated
  158. *
  159. * Satisfy resource requests of each element in the list. Add
  160. * requests that could not satisfied to the failed_list.
  161. */
  162. static void assign_requested_resources_sorted(struct resource_list *head,
  163. struct resource_list_x *fail_head)
  164. {
  165. struct resource *res;
  166. struct resource_list *list;
  167. int idx;
  168. for (list = head->next; list; list = list->next) {
  169. res = list->res;
  170. idx = res - &list->dev->resource[0];
  171. if (resource_size(res) && pci_assign_resource(list->dev, idx)) {
  172. if (fail_head && !pci_is_root_bus(list->dev->bus)) {
  173. /*
  174. * if the failed res is for ROM BAR, and it will
  175. * be enabled later, don't add it to the list
  176. */
  177. if (!((idx == PCI_ROM_RESOURCE) &&
  178. (!(res->flags & IORESOURCE_ROM_ENABLE))))
  179. add_to_failed_list(fail_head, list->dev, res);
  180. }
  181. reset_resource(res);
  182. }
  183. }
  184. }
  185. static void __assign_resources_sorted(struct resource_list *head,
  186. struct resource_list_x *add_head,
  187. struct resource_list_x *fail_head)
  188. {
  189. /* Satisfy the must-have resource requests */
  190. assign_requested_resources_sorted(head, fail_head);
  191. /* Try to satisfy any additional nice-to-have resource
  192. requests */
  193. if (add_head)
  194. adjust_resources_sorted(add_head, head);
  195. free_list(resource_list, head);
  196. }
  197. static void pdev_assign_resources_sorted(struct pci_dev *dev,
  198. struct resource_list_x *fail_head)
  199. {
  200. struct resource_list head;
  201. head.next = NULL;
  202. __dev_sort_resources(dev, &head);
  203. __assign_resources_sorted(&head, NULL, fail_head);
  204. }
  205. static void pbus_assign_resources_sorted(const struct pci_bus *bus,
  206. struct resource_list_x *add_head,
  207. struct resource_list_x *fail_head)
  208. {
  209. struct pci_dev *dev;
  210. struct resource_list head;
  211. head.next = NULL;
  212. list_for_each_entry(dev, &bus->devices, bus_list)
  213. __dev_sort_resources(dev, &head);
  214. __assign_resources_sorted(&head, add_head, fail_head);
  215. }
  216. void pci_setup_cardbus(struct pci_bus *bus)
  217. {
  218. struct pci_dev *bridge = bus->self;
  219. struct resource *res;
  220. struct pci_bus_region region;
  221. dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
  222. bus->secondary, bus->subordinate);
  223. res = bus->resource[0];
  224. pcibios_resource_to_bus(bridge, &region, res);
  225. if (res->flags & IORESOURCE_IO) {
  226. /*
  227. * The IO resource is allocated a range twice as large as it
  228. * would normally need. This allows us to set both IO regs.
  229. */
  230. dev_info(&bridge->dev, " bridge window %pR\n", res);
  231. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  232. region.start);
  233. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  234. region.end);
  235. }
  236. res = bus->resource[1];
  237. pcibios_resource_to_bus(bridge, &region, res);
  238. if (res->flags & IORESOURCE_IO) {
  239. dev_info(&bridge->dev, " bridge window %pR\n", res);
  240. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  241. region.start);
  242. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  243. region.end);
  244. }
  245. res = bus->resource[2];
  246. pcibios_resource_to_bus(bridge, &region, res);
  247. if (res->flags & IORESOURCE_MEM) {
  248. dev_info(&bridge->dev, " bridge window %pR\n", res);
  249. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  250. region.start);
  251. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  252. region.end);
  253. }
  254. res = bus->resource[3];
  255. pcibios_resource_to_bus(bridge, &region, res);
  256. if (res->flags & IORESOURCE_MEM) {
  257. dev_info(&bridge->dev, " bridge window %pR\n", res);
  258. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  259. region.start);
  260. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  261. region.end);
  262. }
  263. }
  264. EXPORT_SYMBOL(pci_setup_cardbus);
  265. /* Initialize bridges with base/limit values we have collected.
  266. PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  267. requires that if there is no I/O ports or memory behind the
  268. bridge, corresponding range must be turned off by writing base
  269. value greater than limit to the bridge's base/limit registers.
  270. Note: care must be taken when updating I/O base/limit registers
  271. of bridges which support 32-bit I/O. This update requires two
  272. config space writes, so it's quite possible that an I/O window of
  273. the bridge will have some undesirable address (e.g. 0) after the
  274. first write. Ditto 64-bit prefetchable MMIO. */
  275. static void pci_setup_bridge_io(struct pci_bus *bus)
  276. {
  277. struct pci_dev *bridge = bus->self;
  278. struct resource *res;
  279. struct pci_bus_region region;
  280. u32 l, io_upper16;
  281. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  282. res = bus->resource[0];
  283. pcibios_resource_to_bus(bridge, &region, res);
  284. if (res->flags & IORESOURCE_IO) {
  285. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  286. l &= 0xffff0000;
  287. l |= (region.start >> 8) & 0x00f0;
  288. l |= region.end & 0xf000;
  289. /* Set up upper 16 bits of I/O base/limit. */
  290. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  291. dev_info(&bridge->dev, " bridge window %pR\n", res);
  292. } else {
  293. /* Clear upper 16 bits of I/O base/limit. */
  294. io_upper16 = 0;
  295. l = 0x00f0;
  296. dev_info(&bridge->dev, " bridge window [io disabled]\n");
  297. }
  298. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  299. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  300. /* Update lower 16 bits of I/O base/limit. */
  301. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  302. /* Update upper 16 bits of I/O base/limit. */
  303. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  304. }
  305. static void pci_setup_bridge_mmio(struct pci_bus *bus)
  306. {
  307. struct pci_dev *bridge = bus->self;
  308. struct resource *res;
  309. struct pci_bus_region region;
  310. u32 l;
  311. /* Set up the top and bottom of the PCI Memory segment for this bus. */
  312. res = bus->resource[1];
  313. pcibios_resource_to_bus(bridge, &region, res);
  314. if (res->flags & IORESOURCE_MEM) {
  315. l = (region.start >> 16) & 0xfff0;
  316. l |= region.end & 0xfff00000;
  317. dev_info(&bridge->dev, " bridge window %pR\n", res);
  318. } else {
  319. l = 0x0000fff0;
  320. dev_info(&bridge->dev, " bridge window [mem disabled]\n");
  321. }
  322. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  323. }
  324. static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
  325. {
  326. struct pci_dev *bridge = bus->self;
  327. struct resource *res;
  328. struct pci_bus_region region;
  329. u32 l, bu, lu;
  330. /* Clear out the upper 32 bits of PREF limit.
  331. If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
  332. disables PREF range, which is ok. */
  333. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  334. /* Set up PREF base/limit. */
  335. bu = lu = 0;
  336. res = bus->resource[2];
  337. pcibios_resource_to_bus(bridge, &region, res);
  338. if (res->flags & IORESOURCE_PREFETCH) {
  339. l = (region.start >> 16) & 0xfff0;
  340. l |= region.end & 0xfff00000;
  341. if (res->flags & IORESOURCE_MEM_64) {
  342. bu = upper_32_bits(region.start);
  343. lu = upper_32_bits(region.end);
  344. }
  345. dev_info(&bridge->dev, " bridge window %pR\n", res);
  346. } else {
  347. l = 0x0000fff0;
  348. dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
  349. }
  350. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  351. /* Set the upper 32 bits of PREF base & limit. */
  352. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
  353. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
  354. }
  355. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  356. {
  357. struct pci_dev *bridge = bus->self;
  358. dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
  359. bus->secondary, bus->subordinate);
  360. if (type & IORESOURCE_IO)
  361. pci_setup_bridge_io(bus);
  362. if (type & IORESOURCE_MEM)
  363. pci_setup_bridge_mmio(bus);
  364. if (type & IORESOURCE_PREFETCH)
  365. pci_setup_bridge_mmio_pref(bus);
  366. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  367. }
  368. static void pci_setup_bridge(struct pci_bus *bus)
  369. {
  370. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  371. IORESOURCE_PREFETCH;
  372. __pci_setup_bridge(bus, type);
  373. }
  374. /* Check whether the bridge supports optional I/O and
  375. prefetchable memory ranges. If not, the respective
  376. base/limit registers must be read-only and read as 0. */
  377. static void pci_bridge_check_ranges(struct pci_bus *bus)
  378. {
  379. u16 io;
  380. u32 pmem;
  381. struct pci_dev *bridge = bus->self;
  382. struct resource *b_res;
  383. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  384. b_res[1].flags |= IORESOURCE_MEM;
  385. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  386. if (!io) {
  387. pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
  388. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  389. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  390. }
  391. if (io)
  392. b_res[0].flags |= IORESOURCE_IO;
  393. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  394. disconnect boundary by one PCI data phase.
  395. Workaround: do not use prefetching on this device. */
  396. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  397. return;
  398. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  399. if (!pmem) {
  400. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  401. 0xfff0fff0);
  402. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  403. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  404. }
  405. if (pmem) {
  406. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  407. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) ==
  408. PCI_PREF_RANGE_TYPE_64) {
  409. b_res[2].flags |= IORESOURCE_MEM_64;
  410. b_res[2].flags |= PCI_PREF_RANGE_TYPE_64;
  411. }
  412. }
  413. /* double check if bridge does support 64 bit pref */
  414. if (b_res[2].flags & IORESOURCE_MEM_64) {
  415. u32 mem_base_hi, tmp;
  416. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  417. &mem_base_hi);
  418. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  419. 0xffffffff);
  420. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  421. if (!tmp)
  422. b_res[2].flags &= ~IORESOURCE_MEM_64;
  423. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  424. mem_base_hi);
  425. }
  426. }
  427. /* Helper function for sizing routines: find first available
  428. bus resource of a given type. Note: we intentionally skip
  429. the bus resources which have already been assigned (that is,
  430. have non-NULL parent resource). */
  431. static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  432. {
  433. int i;
  434. struct resource *r;
  435. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  436. IORESOURCE_PREFETCH;
  437. pci_bus_for_each_resource(bus, r, i) {
  438. if (r == &ioport_resource || r == &iomem_resource)
  439. continue;
  440. if (r && (r->flags & type_mask) == type && !r->parent)
  441. return r;
  442. }
  443. return NULL;
  444. }
  445. static resource_size_t calculate_iosize(resource_size_t size,
  446. resource_size_t min_size,
  447. resource_size_t size1,
  448. resource_size_t old_size,
  449. resource_size_t align)
  450. {
  451. if (size < min_size)
  452. size = min_size;
  453. if (old_size == 1 )
  454. old_size = 0;
  455. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  456. flag in the struct pci_bus. */
  457. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  458. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  459. #endif
  460. size = ALIGN(size + size1, align);
  461. if (size < old_size)
  462. size = old_size;
  463. return size;
  464. }
  465. static resource_size_t calculate_memsize(resource_size_t size,
  466. resource_size_t min_size,
  467. resource_size_t size1,
  468. resource_size_t old_size,
  469. resource_size_t align)
  470. {
  471. if (size < min_size)
  472. size = min_size;
  473. if (old_size == 1 )
  474. old_size = 0;
  475. if (size < old_size)
  476. size = old_size;
  477. size = ALIGN(size + size1, align);
  478. return size;
  479. }
  480. /**
  481. * pbus_size_io() - size the io window of a given bus
  482. *
  483. * @bus : the bus
  484. * @min_size : the minimum io window that must to be allocated
  485. * @add_size : additional optional io window
  486. * @add_head : track the additional io window on this list
  487. *
  488. * Sizing the IO windows of the PCI-PCI bridge is trivial,
  489. * since these windows have 4K granularity and the IO ranges
  490. * of non-bridge PCI devices are limited to 256 bytes.
  491. * We must be careful with the ISA aliasing though.
  492. */
  493. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
  494. resource_size_t add_size, struct resource_list_x *add_head)
  495. {
  496. struct pci_dev *dev;
  497. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  498. unsigned long size = 0, size0 = 0, size1 = 0;
  499. if (!b_res)
  500. return;
  501. list_for_each_entry(dev, &bus->devices, bus_list) {
  502. int i;
  503. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  504. struct resource *r = &dev->resource[i];
  505. unsigned long r_size;
  506. if (r->parent || !(r->flags & IORESOURCE_IO))
  507. continue;
  508. r_size = resource_size(r);
  509. if (r_size < 0x400)
  510. /* Might be re-aligned for ISA */
  511. size += r_size;
  512. else
  513. size1 += r_size;
  514. }
  515. }
  516. size0 = calculate_iosize(size, min_size, size1,
  517. resource_size(b_res), 4096);
  518. size1 = !add_size? size0:
  519. calculate_iosize(size, min_size+add_size, size1,
  520. resource_size(b_res), 4096);
  521. if (!size0 && !size1) {
  522. if (b_res->start || b_res->end)
  523. dev_info(&bus->self->dev, "disabling bridge window "
  524. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  525. bus->secondary, bus->subordinate);
  526. b_res->flags = 0;
  527. return;
  528. }
  529. /* Alignment of the IO window is always 4K */
  530. b_res->start = 4096;
  531. b_res->end = b_res->start + size0 - 1;
  532. b_res->flags |= IORESOURCE_STARTALIGN;
  533. if (size1 > size0 && add_head)
  534. add_to_list(add_head, bus->self, b_res, size1-size0);
  535. }
  536. /**
  537. * pbus_size_mem() - size the memory window of a given bus
  538. *
  539. * @bus : the bus
  540. * @min_size : the minimum memory window that must to be allocated
  541. * @add_size : additional optional memory window
  542. * @add_head : track the additional memory window on this list
  543. *
  544. * Calculate the size of the bus and minimal alignment which
  545. * guarantees that all child resources fit in this size.
  546. */
  547. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  548. unsigned long type, resource_size_t min_size,
  549. resource_size_t add_size,
  550. struct resource_list_x *add_head)
  551. {
  552. struct pci_dev *dev;
  553. resource_size_t min_align, align, size, size0, size1;
  554. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  555. int order, max_order;
  556. struct resource *b_res = find_free_bus_resource(bus, type);
  557. unsigned int mem64_mask = 0;
  558. if (!b_res)
  559. return 0;
  560. memset(aligns, 0, sizeof(aligns));
  561. max_order = 0;
  562. size = 0;
  563. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  564. b_res->flags &= ~IORESOURCE_MEM_64;
  565. list_for_each_entry(dev, &bus->devices, bus_list) {
  566. int i;
  567. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  568. struct resource *r = &dev->resource[i];
  569. resource_size_t r_size;
  570. if (r->parent || (r->flags & mask) != type)
  571. continue;
  572. r_size = resource_size(r);
  573. /* For bridges size != alignment */
  574. align = pci_resource_alignment(dev, r);
  575. order = __ffs(align) - 20;
  576. if (order > 11) {
  577. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  578. "(bad alignment %#llx)\n", i, r,
  579. (unsigned long long) align);
  580. r->flags = 0;
  581. continue;
  582. }
  583. size += r_size;
  584. if (order < 0)
  585. order = 0;
  586. /* Exclude ranges with size > align from
  587. calculation of the alignment. */
  588. if (r_size == align)
  589. aligns[order] += align;
  590. if (order > max_order)
  591. max_order = order;
  592. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  593. }
  594. }
  595. align = 0;
  596. min_align = 0;
  597. for (order = 0; order <= max_order; order++) {
  598. resource_size_t align1 = 1;
  599. align1 <<= (order + 20);
  600. if (!align)
  601. min_align = align1;
  602. else if (ALIGN(align + min_align, min_align) < align1)
  603. min_align = align1 >> 1;
  604. align += aligns[order];
  605. }
  606. size0 = calculate_memsize(size, min_size, 0, resource_size(b_res), align);
  607. size1 = !add_size ? size :
  608. calculate_memsize(size, min_size+add_size, 0,
  609. resource_size(b_res), align);
  610. if (!size0 && !size1) {
  611. if (b_res->start || b_res->end)
  612. dev_info(&bus->self->dev, "disabling bridge window "
  613. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  614. bus->secondary, bus->subordinate);
  615. b_res->flags = 0;
  616. return 1;
  617. }
  618. b_res->start = min_align;
  619. b_res->end = size0 + min_align - 1;
  620. b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
  621. if (size1 > size0 && add_head)
  622. add_to_list(add_head, bus->self, b_res, size1-size0);
  623. return 1;
  624. }
  625. static void pci_bus_size_cardbus(struct pci_bus *bus)
  626. {
  627. struct pci_dev *bridge = bus->self;
  628. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  629. u16 ctrl;
  630. /*
  631. * Reserve some resources for CardBus. We reserve
  632. * a fixed amount of bus space for CardBus bridges.
  633. */
  634. b_res[0].start = 0;
  635. b_res[0].end = pci_cardbus_io_size - 1;
  636. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  637. b_res[1].start = 0;
  638. b_res[1].end = pci_cardbus_io_size - 1;
  639. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  640. /*
  641. * Check whether prefetchable memory is supported
  642. * by this bridge.
  643. */
  644. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  645. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  646. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  647. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  648. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  649. }
  650. /*
  651. * If we have prefetchable memory support, allocate
  652. * two regions. Otherwise, allocate one region of
  653. * twice the size.
  654. */
  655. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  656. b_res[2].start = 0;
  657. b_res[2].end = pci_cardbus_mem_size - 1;
  658. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
  659. b_res[3].start = 0;
  660. b_res[3].end = pci_cardbus_mem_size - 1;
  661. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  662. } else {
  663. b_res[3].start = 0;
  664. b_res[3].end = pci_cardbus_mem_size * 2 - 1;
  665. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  666. }
  667. }
  668. void __ref __pci_bus_size_bridges(struct pci_bus *bus,
  669. struct resource_list_x *add_head)
  670. {
  671. struct pci_dev *dev;
  672. unsigned long mask, prefmask;
  673. resource_size_t additional_mem_size = 0, additional_io_size = 0;
  674. list_for_each_entry(dev, &bus->devices, bus_list) {
  675. struct pci_bus *b = dev->subordinate;
  676. if (!b)
  677. continue;
  678. switch (dev->class >> 8) {
  679. case PCI_CLASS_BRIDGE_CARDBUS:
  680. pci_bus_size_cardbus(b);
  681. break;
  682. case PCI_CLASS_BRIDGE_PCI:
  683. default:
  684. __pci_bus_size_bridges(b, add_head);
  685. break;
  686. }
  687. }
  688. /* The root bus? */
  689. if (!bus->self)
  690. return;
  691. switch (bus->self->class >> 8) {
  692. case PCI_CLASS_BRIDGE_CARDBUS:
  693. /* don't size cardbuses yet. */
  694. break;
  695. case PCI_CLASS_BRIDGE_PCI:
  696. pci_bridge_check_ranges(bus);
  697. if (bus->self->is_hotplug_bridge) {
  698. additional_io_size = pci_hotplug_io_size;
  699. additional_mem_size = pci_hotplug_mem_size;
  700. }
  701. /*
  702. * Follow thru
  703. */
  704. default:
  705. pbus_size_io(bus, 0, additional_io_size, add_head);
  706. /* If the bridge supports prefetchable range, size it
  707. separately. If it doesn't, or its prefetchable window
  708. has already been allocated by arch code, try
  709. non-prefetchable range for both types of PCI memory
  710. resources. */
  711. mask = IORESOURCE_MEM;
  712. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  713. if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, add_head))
  714. mask = prefmask; /* Success, size non-prefetch only. */
  715. else
  716. additional_mem_size += additional_mem_size;
  717. pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, add_head);
  718. break;
  719. }
  720. }
  721. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  722. {
  723. __pci_bus_size_bridges(bus, NULL);
  724. }
  725. EXPORT_SYMBOL(pci_bus_size_bridges);
  726. static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
  727. struct resource_list_x *add_head,
  728. struct resource_list_x *fail_head)
  729. {
  730. struct pci_bus *b;
  731. struct pci_dev *dev;
  732. pbus_assign_resources_sorted(bus, add_head, fail_head);
  733. list_for_each_entry(dev, &bus->devices, bus_list) {
  734. b = dev->subordinate;
  735. if (!b)
  736. continue;
  737. __pci_bus_assign_resources(b, add_head, fail_head);
  738. switch (dev->class >> 8) {
  739. case PCI_CLASS_BRIDGE_PCI:
  740. if (!pci_is_enabled(dev))
  741. pci_setup_bridge(b);
  742. break;
  743. case PCI_CLASS_BRIDGE_CARDBUS:
  744. pci_setup_cardbus(b);
  745. break;
  746. default:
  747. dev_info(&dev->dev, "not setting up bridge for bus "
  748. "%04x:%02x\n", pci_domain_nr(b), b->number);
  749. break;
  750. }
  751. }
  752. }
  753. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  754. {
  755. __pci_bus_assign_resources(bus, NULL, NULL);
  756. }
  757. EXPORT_SYMBOL(pci_bus_assign_resources);
  758. static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
  759. struct resource_list_x *fail_head)
  760. {
  761. struct pci_bus *b;
  762. pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
  763. b = bridge->subordinate;
  764. if (!b)
  765. return;
  766. __pci_bus_assign_resources(b, NULL, fail_head);
  767. switch (bridge->class >> 8) {
  768. case PCI_CLASS_BRIDGE_PCI:
  769. pci_setup_bridge(b);
  770. break;
  771. case PCI_CLASS_BRIDGE_CARDBUS:
  772. pci_setup_cardbus(b);
  773. break;
  774. default:
  775. dev_info(&bridge->dev, "not setting up bridge for bus "
  776. "%04x:%02x\n", pci_domain_nr(b), b->number);
  777. break;
  778. }
  779. }
  780. static void pci_bridge_release_resources(struct pci_bus *bus,
  781. unsigned long type)
  782. {
  783. int idx;
  784. bool changed = false;
  785. struct pci_dev *dev;
  786. struct resource *r;
  787. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  788. IORESOURCE_PREFETCH;
  789. dev = bus->self;
  790. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  791. idx++) {
  792. r = &dev->resource[idx];
  793. if ((r->flags & type_mask) != type)
  794. continue;
  795. if (!r->parent)
  796. continue;
  797. /*
  798. * if there are children under that, we should release them
  799. * all
  800. */
  801. release_child_resources(r);
  802. if (!release_resource(r)) {
  803. dev_printk(KERN_DEBUG, &dev->dev,
  804. "resource %d %pR released\n", idx, r);
  805. /* keep the old size */
  806. r->end = resource_size(r) - 1;
  807. r->start = 0;
  808. r->flags = 0;
  809. changed = true;
  810. }
  811. }
  812. if (changed) {
  813. /* avoiding touch the one without PREF */
  814. if (type & IORESOURCE_PREFETCH)
  815. type = IORESOURCE_PREFETCH;
  816. __pci_setup_bridge(bus, type);
  817. }
  818. }
  819. enum release_type {
  820. leaf_only,
  821. whole_subtree,
  822. };
  823. /*
  824. * try to release pci bridge resources that is from leaf bridge,
  825. * so we can allocate big new one later
  826. */
  827. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  828. unsigned long type,
  829. enum release_type rel_type)
  830. {
  831. struct pci_dev *dev;
  832. bool is_leaf_bridge = true;
  833. list_for_each_entry(dev, &bus->devices, bus_list) {
  834. struct pci_bus *b = dev->subordinate;
  835. if (!b)
  836. continue;
  837. is_leaf_bridge = false;
  838. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  839. continue;
  840. if (rel_type == whole_subtree)
  841. pci_bus_release_bridge_resources(b, type,
  842. whole_subtree);
  843. }
  844. if (pci_is_root_bus(bus))
  845. return;
  846. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  847. return;
  848. if ((rel_type == whole_subtree) || is_leaf_bridge)
  849. pci_bridge_release_resources(bus, type);
  850. }
  851. static void pci_bus_dump_res(struct pci_bus *bus)
  852. {
  853. struct resource *res;
  854. int i;
  855. pci_bus_for_each_resource(bus, res, i) {
  856. if (!res || !res->end || !res->flags)
  857. continue;
  858. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  859. }
  860. }
  861. static void pci_bus_dump_resources(struct pci_bus *bus)
  862. {
  863. struct pci_bus *b;
  864. struct pci_dev *dev;
  865. pci_bus_dump_res(bus);
  866. list_for_each_entry(dev, &bus->devices, bus_list) {
  867. b = dev->subordinate;
  868. if (!b)
  869. continue;
  870. pci_bus_dump_resources(b);
  871. }
  872. }
  873. void __init
  874. pci_assign_unassigned_resources(void)
  875. {
  876. struct pci_bus *bus;
  877. struct resource_list_x add_list; /* list of resources that
  878. want additional resources */
  879. add_list.next = NULL;
  880. /* Depth first, calculate sizes and alignments of all
  881. subordinate buses. */
  882. list_for_each_entry(bus, &pci_root_buses, node) {
  883. __pci_bus_size_bridges(bus, &add_list);
  884. }
  885. /* Depth last, allocate resources and update the hardware. */
  886. list_for_each_entry(bus, &pci_root_buses, node) {
  887. __pci_bus_assign_resources(bus, &add_list, NULL);
  888. pci_enable_bridges(bus);
  889. }
  890. BUG_ON(add_list.next);
  891. /* dump the resource on buses */
  892. list_for_each_entry(bus, &pci_root_buses, node) {
  893. pci_bus_dump_resources(bus);
  894. }
  895. }
  896. void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  897. {
  898. struct pci_bus *parent = bridge->subordinate;
  899. int tried_times = 0;
  900. struct resource_list_x head, *list;
  901. int retval;
  902. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  903. IORESOURCE_PREFETCH;
  904. head.next = NULL;
  905. again:
  906. pci_bus_size_bridges(parent);
  907. __pci_bridge_assign_resources(bridge, &head);
  908. tried_times++;
  909. if (!head.next)
  910. goto enable_all;
  911. if (tried_times >= 2) {
  912. /* still fail, don't need to try more */
  913. free_list(resource_list_x, &head);
  914. goto enable_all;
  915. }
  916. printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
  917. tried_times + 1);
  918. /*
  919. * Try to release leaf bridge's resources that doesn't fit resource of
  920. * child device under that bridge
  921. */
  922. for (list = head.next; list;) {
  923. struct pci_bus *bus = list->dev->bus;
  924. unsigned long flags = list->flags;
  925. pci_bus_release_bridge_resources(bus, flags & type_mask,
  926. whole_subtree);
  927. list = list->next;
  928. }
  929. /* restore size and flags */
  930. for (list = head.next; list;) {
  931. struct resource *res = list->res;
  932. res->start = list->start;
  933. res->end = list->end;
  934. res->flags = list->flags;
  935. if (list->dev->subordinate)
  936. res->flags = 0;
  937. list = list->next;
  938. }
  939. free_list(resource_list_x, &head);
  940. goto again;
  941. enable_all:
  942. retval = pci_reenable_device(bridge);
  943. pci_set_master(bridge);
  944. pci_enable_bridges(parent);
  945. }
  946. EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);