aperture_64.c 14 KB

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