aperture_64.c 13 KB

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