aperture_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Firmware replacement code.
  3. *
  4. * Work around broken BIOSes that don't set an aperture, only set the
  5. * aperture in the AGP bridge, or set too small aperture.
  6. *
  7. * If all fails map the aperture over some low memory. This is cheaper than
  8. * doing bounce buffering. The memory is lost. This is done at early boot
  9. * because only the bootmem allocator can allocate 32+MB.
  10. *
  11. * Copyright 2002 Andi Kleen, SuSE Labs.
  12. */
  13. #define pr_fmt(fmt) "AGP: " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/init.h>
  17. #include <linux/memblock.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/pci_ids.h>
  20. #include <linux/pci.h>
  21. #include <linux/bitops.h>
  22. #include <linux/suspend.h>
  23. #include <asm/e820.h>
  24. #include <asm/io.h>
  25. #include <asm/iommu.h>
  26. #include <asm/gart.h>
  27. #include <asm/pci-direct.h>
  28. #include <asm/dma.h>
  29. #include <asm/amd_nb.h>
  30. #include <asm/x86_init.h>
  31. /*
  32. * Using 512M as goal, in case kexec will load kernel_big
  33. * that will do the on-position decompress, and could overlap with
  34. * with the gart aperture that is used.
  35. * Sequence:
  36. * kernel_small
  37. * ==> kexec (with kdump trigger path or gart still enabled)
  38. * ==> kernel_small (gart area become e820_reserved)
  39. * ==> kexec (with kdump trigger path or gart still enabled)
  40. * ==> kerne_big (uncompressed size will be big than 64M or 128M)
  41. * So don't use 512M below as gart iommu, leave the space for kernel
  42. * code for safe.
  43. */
  44. #define GART_MIN_ADDR (512ULL << 20)
  45. #define GART_MAX_ADDR (1ULL << 32)
  46. int gart_iommu_aperture;
  47. int gart_iommu_aperture_disabled __initdata;
  48. int gart_iommu_aperture_allowed __initdata;
  49. int fallback_aper_order __initdata = 1; /* 64MB */
  50. int fallback_aper_force __initdata;
  51. int fix_aperture __initdata = 1;
  52. /* This code runs before the PCI subsystem is initialized, so just
  53. access the northbridge directly. */
  54. static u32 __init allocate_aperture(void)
  55. {
  56. u32 aper_size;
  57. unsigned long addr;
  58. /* aper_size should <= 1G */
  59. if (fallback_aper_order > 5)
  60. fallback_aper_order = 5;
  61. aper_size = (32 * 1024 * 1024) << fallback_aper_order;
  62. /*
  63. * Aperture has to be naturally aligned. This means a 2GB aperture
  64. * won't have much chance of finding a place in the lower 4GB of
  65. * memory. Unfortunately we cannot move it up because that would
  66. * make the IOMMU useless.
  67. */
  68. addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
  69. aper_size, aper_size);
  70. if (!addr) {
  71. pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
  72. addr, addr + aper_size - 1, aper_size >> 10);
  73. return 0;
  74. }
  75. memblock_reserve(addr, aper_size);
  76. pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
  77. addr, addr + aper_size - 1, aper_size >> 10);
  78. register_nosave_region(addr >> PAGE_SHIFT,
  79. (addr+aper_size) >> PAGE_SHIFT);
  80. return (u32)addr;
  81. }
  82. /* Find a PCI capability */
  83. static u32 __init find_cap(int bus, int slot, int func, int cap)
  84. {
  85. int bytes;
  86. u8 pos;
  87. if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
  88. PCI_STATUS_CAP_LIST))
  89. return 0;
  90. pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
  91. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  92. u8 id;
  93. pos &= ~3;
  94. id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
  95. if (id == 0xff)
  96. break;
  97. if (id == cap)
  98. return pos;
  99. pos = read_pci_config_byte(bus, slot, func,
  100. pos+PCI_CAP_LIST_NEXT);
  101. }
  102. return 0;
  103. }
  104. /* Read a standard AGPv3 bridge header */
  105. static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
  106. {
  107. u32 apsize;
  108. u32 apsizereg;
  109. int nbits;
  110. u32 aper_low, aper_hi;
  111. u64 aper;
  112. u32 old_order;
  113. pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func);
  114. apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
  115. if (apsizereg == 0xffffffff) {
  116. pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n",
  117. bus, slot, func);
  118. return 0;
  119. }
  120. /* old_order could be the value from NB gart setting */
  121. old_order = *order;
  122. apsize = apsizereg & 0xfff;
  123. /* Some BIOS use weird encodings not in the AGPv3 table. */
  124. if (apsize & 0xff)
  125. apsize |= 0xf00;
  126. nbits = hweight16(apsize);
  127. *order = 7 - nbits;
  128. if ((int)*order < 0) /* < 32MB */
  129. *order = 0;
  130. aper_low = read_pci_config(bus, slot, func, 0x10);
  131. aper_hi = read_pci_config(bus, slot, func, 0x14);
  132. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  133. /*
  134. * On some sick chips, APSIZE is 0. It means it wants 4G
  135. * so let double check that order, and lets trust AMD NB settings:
  136. */
  137. pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n",
  138. bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1,
  139. 32 << old_order);
  140. if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
  141. pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n",
  142. bus, slot, func, 32 << *order, apsizereg);
  143. *order = old_order;
  144. }
  145. pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n",
  146. bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1,
  147. 32 << *order, apsizereg);
  148. if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
  149. return 0;
  150. return (u32)aper;
  151. }
  152. /*
  153. * Look for an AGP bridge. Windows only expects the aperture in the
  154. * AGP bridge and some BIOS forget to initialize the Northbridge too.
  155. * Work around this here.
  156. *
  157. * Do an PCI bus scan by hand because we're running before the PCI
  158. * subsystem.
  159. *
  160. * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
  161. * generically. It's probably overkill to always scan all slots because
  162. * the AGP bridges should be always an own bus on the HT hierarchy,
  163. * but do it here for future safety.
  164. */
  165. static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
  166. {
  167. int bus, slot, func;
  168. /* Poor man's PCI discovery */
  169. for (bus = 0; bus < 256; bus++) {
  170. for (slot = 0; slot < 32; slot++) {
  171. for (func = 0; func < 8; func++) {
  172. u32 class, cap;
  173. u8 type;
  174. class = read_pci_config(bus, slot, func,
  175. PCI_CLASS_REVISION);
  176. if (class == 0xffffffff)
  177. break;
  178. switch (class >> 16) {
  179. case PCI_CLASS_BRIDGE_HOST:
  180. case PCI_CLASS_BRIDGE_OTHER: /* needed? */
  181. /* AGP bridge? */
  182. cap = find_cap(bus, slot, func,
  183. PCI_CAP_ID_AGP);
  184. if (!cap)
  185. break;
  186. *valid_agp = 1;
  187. return read_agp(bus, slot, func, cap,
  188. order);
  189. }
  190. /* No multi-function device? */
  191. type = read_pci_config_byte(bus, slot, func,
  192. PCI_HEADER_TYPE);
  193. if (!(type & 0x80))
  194. break;
  195. }
  196. }
  197. }
  198. pr_info("No AGP bridge found\n");
  199. return 0;
  200. }
  201. static int gart_fix_e820 __initdata = 1;
  202. static int __init parse_gart_mem(char *p)
  203. {
  204. if (!p)
  205. return -EINVAL;
  206. if (!strncmp(p, "off", 3))
  207. gart_fix_e820 = 0;
  208. else if (!strncmp(p, "on", 2))
  209. gart_fix_e820 = 1;
  210. return 0;
  211. }
  212. early_param("gart_fix_e820", parse_gart_mem);
  213. void __init early_gart_iommu_check(void)
  214. {
  215. /*
  216. * in case it is enabled before, esp for kexec/kdump,
  217. * previous kernel already enable that. memset called
  218. * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
  219. * or second kernel have different position for GART hole. and new
  220. * kernel could use hole as RAM that is still used by GART set by
  221. * first kernel
  222. * or BIOS forget to put that in reserved.
  223. * try to update e820 to make that region as reserved.
  224. */
  225. u32 agp_aper_order = 0;
  226. int i, fix, slot, valid_agp = 0;
  227. u32 ctl;
  228. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  229. u64 aper_base = 0, last_aper_base = 0;
  230. int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
  231. if (!amd_gart_present())
  232. return;
  233. if (!early_pci_allowed())
  234. return;
  235. /* This is mostly duplicate of iommu_hole_init */
  236. search_agp_bridge(&agp_aper_order, &valid_agp);
  237. fix = 0;
  238. for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  239. int bus;
  240. int dev_base, dev_limit;
  241. bus = amd_nb_bus_dev_ranges[i].bus;
  242. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  243. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  244. for (slot = dev_base; slot < dev_limit; slot++) {
  245. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  246. continue;
  247. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  248. aper_enabled = ctl & GARTEN;
  249. aper_order = (ctl >> 1) & 7;
  250. aper_size = (32 * 1024 * 1024) << aper_order;
  251. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  252. aper_base <<= 25;
  253. if (last_valid) {
  254. if ((aper_order != last_aper_order) ||
  255. (aper_base != last_aper_base) ||
  256. (aper_enabled != last_aper_enabled)) {
  257. fix = 1;
  258. break;
  259. }
  260. }
  261. last_aper_order = aper_order;
  262. last_aper_base = aper_base;
  263. last_aper_enabled = aper_enabled;
  264. last_valid = 1;
  265. }
  266. }
  267. if (!fix && !aper_enabled)
  268. return;
  269. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  270. fix = 1;
  271. if (gart_fix_e820 && !fix && aper_enabled) {
  272. if (e820_any_mapped(aper_base, aper_base + aper_size,
  273. E820_RAM)) {
  274. /* reserve it, so we can reuse it in second kernel */
  275. pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
  276. aper_base, aper_base + aper_size - 1);
  277. e820_add_region(aper_base, aper_size, E820_RESERVED);
  278. update_e820();
  279. }
  280. }
  281. if (valid_agp)
  282. return;
  283. /* disable them all at first */
  284. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  285. int bus;
  286. int dev_base, dev_limit;
  287. bus = amd_nb_bus_dev_ranges[i].bus;
  288. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  289. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  290. for (slot = dev_base; slot < dev_limit; slot++) {
  291. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  292. continue;
  293. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  294. ctl &= ~GARTEN;
  295. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  296. }
  297. }
  298. }
  299. static int __initdata printed_gart_size_msg;
  300. int __init gart_iommu_hole_init(void)
  301. {
  302. u32 agp_aper_base = 0, agp_aper_order = 0;
  303. u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
  304. u64 aper_base, last_aper_base = 0;
  305. int fix, slot, valid_agp = 0;
  306. int i, node;
  307. if (!amd_gart_present())
  308. return -ENODEV;
  309. if (gart_iommu_aperture_disabled || !fix_aperture ||
  310. !early_pci_allowed())
  311. return -ENODEV;
  312. pr_info("Checking aperture...\n");
  313. if (!fallback_aper_force)
  314. agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
  315. fix = 0;
  316. node = 0;
  317. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  318. int bus;
  319. int dev_base, dev_limit;
  320. u32 ctl;
  321. bus = amd_nb_bus_dev_ranges[i].bus;
  322. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  323. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  324. for (slot = dev_base; slot < dev_limit; slot++) {
  325. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  326. continue;
  327. iommu_detected = 1;
  328. gart_iommu_aperture = 1;
  329. x86_init.iommu.iommu_init = gart_iommu_init;
  330. ctl = read_pci_config(bus, slot, 3,
  331. AMD64_GARTAPERTURECTL);
  332. /*
  333. * Before we do anything else disable the GART. It may
  334. * still be enabled if we boot into a crash-kernel here.
  335. * Reconfiguring the GART while it is enabled could have
  336. * unknown side-effects.
  337. */
  338. ctl &= ~GARTEN;
  339. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  340. aper_order = (ctl >> 1) & 7;
  341. aper_size = (32 * 1024 * 1024) << aper_order;
  342. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  343. aper_base <<= 25;
  344. pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
  345. node, aper_base, aper_base + aper_size - 1,
  346. aper_size >> 20);
  347. node++;
  348. if (!aperture_valid(aper_base, aper_size, 64<<20)) {
  349. if (valid_agp && agp_aper_base &&
  350. agp_aper_base == aper_base &&
  351. agp_aper_order == aper_order) {
  352. /* the same between two setting from NB and agp */
  353. if (!no_iommu &&
  354. max_pfn > MAX_DMA32_PFN &&
  355. !printed_gart_size_msg) {
  356. pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
  357. pr_err("please increase GART size in your BIOS setup\n");
  358. pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
  359. printed_gart_size_msg = 1;
  360. }
  361. } else {
  362. fix = 1;
  363. goto out;
  364. }
  365. }
  366. if ((last_aper_order && aper_order != last_aper_order) ||
  367. (last_aper_base && aper_base != last_aper_base)) {
  368. fix = 1;
  369. goto out;
  370. }
  371. last_aper_order = aper_order;
  372. last_aper_base = aper_base;
  373. }
  374. }
  375. out:
  376. if (!fix && !fallback_aper_force) {
  377. if (last_aper_base)
  378. return 1;
  379. return 0;
  380. }
  381. if (!fallback_aper_force) {
  382. aper_alloc = agp_aper_base;
  383. aper_order = agp_aper_order;
  384. }
  385. if (aper_alloc) {
  386. /* Got the aperture from the AGP bridge */
  387. } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
  388. force_iommu ||
  389. valid_agp ||
  390. fallback_aper_force) {
  391. pr_info("Your BIOS doesn't leave an aperture memory hole\n");
  392. pr_info("Please enable the IOMMU option in the BIOS setup\n");
  393. pr_info("This costs you %dMB of RAM\n",
  394. 32 << fallback_aper_order);
  395. aper_order = fallback_aper_order;
  396. aper_alloc = allocate_aperture();
  397. if (!aper_alloc) {
  398. /*
  399. * Could disable AGP and IOMMU here, but it's
  400. * probably not worth it. But the later users
  401. * cannot deal with bad apertures and turning
  402. * on the aperture over memory causes very
  403. * strange problems, so it's better to panic
  404. * early.
  405. */
  406. panic("Not enough memory for aperture");
  407. }
  408. } else {
  409. return 0;
  410. }
  411. /* Fix up the north bridges */
  412. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  413. int bus, dev_base, dev_limit;
  414. /*
  415. * Don't enable translation yet but enable GART IO and CPU
  416. * accesses and set DISTLBWALKPRB since GART table memory is UC.
  417. */
  418. u32 ctl = aper_order << 1;
  419. bus = amd_nb_bus_dev_ranges[i].bus;
  420. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  421. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  422. for (slot = dev_base; slot < dev_limit; slot++) {
  423. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  424. continue;
  425. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  426. write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
  427. }
  428. }
  429. set_up_gart_resume(aper_order, aper_alloc);
  430. return 1;
  431. }