aperture_64.c 14 KB

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