aperture_64.c 14 KB

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