pci-gart_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Dynamic DMA mapping support for AMD Hammer.
  3. *
  4. * Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI.
  5. * This allows to use PCI devices that only support 32bit addresses on systems
  6. * with more than 4GB.
  7. *
  8. * See Documentation/DMA-mapping.txt for the interface specification.
  9. *
  10. * Copyright 2002 Andi Kleen, SuSE Labs.
  11. * Subject to the GNU General Public License v2 only.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/ctype.h>
  15. #include <linux/agp_backend.h>
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/pci.h>
  21. #include <linux/module.h>
  22. #include <linux/topology.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bitops.h>
  25. #include <linux/kdebug.h>
  26. #include <linux/scatterlist.h>
  27. #include <asm/atomic.h>
  28. #include <asm/io.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/proto.h>
  32. #include <asm/iommu.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/swiotlb.h>
  35. #include <asm/dma.h>
  36. #include <asm/k8.h>
  37. unsigned long iommu_bus_base; /* GART remapping area (physical) */
  38. static unsigned long iommu_size; /* size of remapping area bytes */
  39. static unsigned long iommu_pages; /* .. and in pages */
  40. u32 *iommu_gatt_base; /* Remapping table */
  41. /* If this is disabled the IOMMU will use an optimized flushing strategy
  42. of only flushing when an mapping is reused. With it true the GART is flushed
  43. for every mapping. Problem is that doing the lazy flush seems to trigger
  44. bugs with some popular PCI cards, in particular 3ware (but has been also
  45. also seen with Qlogic at least). */
  46. int iommu_fullflush = 1;
  47. /* Allocation bitmap for the remapping area */
  48. static DEFINE_SPINLOCK(iommu_bitmap_lock);
  49. static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */
  50. static u32 gart_unmapped_entry;
  51. #define GPTE_VALID 1
  52. #define GPTE_COHERENT 2
  53. #define GPTE_ENCODE(x) \
  54. (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
  55. #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
  56. #define to_pages(addr,size) \
  57. (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
  58. #define EMERGENCY_PAGES 32 /* = 128KB */
  59. #ifdef CONFIG_AGP
  60. #define AGPEXTERN extern
  61. #else
  62. #define AGPEXTERN
  63. #endif
  64. /* backdoor interface to AGP driver */
  65. AGPEXTERN int agp_memory_reserved;
  66. AGPEXTERN __u32 *agp_gatt_table;
  67. static unsigned long next_bit; /* protected by iommu_bitmap_lock */
  68. static int need_flush; /* global flush state. set for each gart wrap */
  69. static unsigned long alloc_iommu(int size)
  70. {
  71. unsigned long offset, flags;
  72. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  73. offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size);
  74. if (offset == -1) {
  75. need_flush = 1;
  76. offset = find_next_zero_string(iommu_gart_bitmap,0,iommu_pages,size);
  77. }
  78. if (offset != -1) {
  79. set_bit_string(iommu_gart_bitmap, offset, size);
  80. next_bit = offset+size;
  81. if (next_bit >= iommu_pages) {
  82. next_bit = 0;
  83. need_flush = 1;
  84. }
  85. }
  86. if (iommu_fullflush)
  87. need_flush = 1;
  88. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  89. return offset;
  90. }
  91. static void free_iommu(unsigned long offset, int size)
  92. {
  93. unsigned long flags;
  94. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  95. __clear_bit_string(iommu_gart_bitmap, offset, size);
  96. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  97. }
  98. /*
  99. * Use global flush state to avoid races with multiple flushers.
  100. */
  101. static void flush_gart(void)
  102. {
  103. unsigned long flags;
  104. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  105. if (need_flush) {
  106. k8_flush_garts();
  107. need_flush = 0;
  108. }
  109. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  110. }
  111. #ifdef CONFIG_IOMMU_LEAK
  112. #define SET_LEAK(x) if (iommu_leak_tab) \
  113. iommu_leak_tab[x] = __builtin_return_address(0);
  114. #define CLEAR_LEAK(x) if (iommu_leak_tab) \
  115. iommu_leak_tab[x] = NULL;
  116. /* Debugging aid for drivers that don't free their IOMMU tables */
  117. static void **iommu_leak_tab;
  118. static int leak_trace;
  119. int iommu_leak_pages = 20;
  120. void dump_leak(void)
  121. {
  122. int i;
  123. static int dump;
  124. if (dump || !iommu_leak_tab) return;
  125. dump = 1;
  126. show_stack(NULL,NULL);
  127. /* Very crude. dump some from the end of the table too */
  128. printk("Dumping %d pages from end of IOMMU:\n", iommu_leak_pages);
  129. for (i = 0; i < iommu_leak_pages; i+=2) {
  130. printk("%lu: ", iommu_pages-i);
  131. printk_address((unsigned long) iommu_leak_tab[iommu_pages-i]);
  132. printk("%c", (i+1)%2 == 0 ? '\n' : ' ');
  133. }
  134. printk("\n");
  135. }
  136. #else
  137. #define SET_LEAK(x)
  138. #define CLEAR_LEAK(x)
  139. #endif
  140. static void iommu_full(struct device *dev, size_t size, int dir)
  141. {
  142. /*
  143. * Ran out of IOMMU space for this operation. This is very bad.
  144. * Unfortunately the drivers cannot handle this operation properly.
  145. * Return some non mapped prereserved space in the aperture and
  146. * let the Northbridge deal with it. This will result in garbage
  147. * in the IO operation. When the size exceeds the prereserved space
  148. * memory corruption will occur or random memory will be DMAed
  149. * out. Hopefully no network devices use single mappings that big.
  150. */
  151. printk(KERN_ERR
  152. "PCI-DMA: Out of IOMMU space for %lu bytes at device %s\n",
  153. size, dev->bus_id);
  154. if (size > PAGE_SIZE*EMERGENCY_PAGES) {
  155. if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  156. panic("PCI-DMA: Memory would be corrupted\n");
  157. if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  158. panic(KERN_ERR "PCI-DMA: Random memory would be DMAed\n");
  159. }
  160. #ifdef CONFIG_IOMMU_LEAK
  161. dump_leak();
  162. #endif
  163. }
  164. static inline int need_iommu(struct device *dev, unsigned long addr, size_t size)
  165. {
  166. u64 mask = *dev->dma_mask;
  167. int high = addr + size > mask;
  168. int mmu = high;
  169. if (force_iommu)
  170. mmu = 1;
  171. return mmu;
  172. }
  173. static inline int nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
  174. {
  175. u64 mask = *dev->dma_mask;
  176. int high = addr + size > mask;
  177. int mmu = high;
  178. return mmu;
  179. }
  180. /* Map a single continuous physical area into the IOMMU.
  181. * Caller needs to check if the iommu is needed and flush.
  182. */
  183. static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
  184. size_t size, int dir)
  185. {
  186. unsigned long npages = to_pages(phys_mem, size);
  187. unsigned long iommu_page = alloc_iommu(npages);
  188. int i;
  189. if (iommu_page == -1) {
  190. if (!nonforced_iommu(dev, phys_mem, size))
  191. return phys_mem;
  192. if (panic_on_overflow)
  193. panic("dma_map_area overflow %lu bytes\n", size);
  194. iommu_full(dev, size, dir);
  195. return bad_dma_address;
  196. }
  197. for (i = 0; i < npages; i++) {
  198. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
  199. SET_LEAK(iommu_page + i);
  200. phys_mem += PAGE_SIZE;
  201. }
  202. return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
  203. }
  204. static dma_addr_t gart_map_simple(struct device *dev, char *buf,
  205. size_t size, int dir)
  206. {
  207. dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir);
  208. flush_gart();
  209. return map;
  210. }
  211. /* Map a single area into the IOMMU */
  212. static dma_addr_t gart_map_single(struct device *dev, void *addr, size_t size, int dir)
  213. {
  214. unsigned long phys_mem, bus;
  215. if (!dev)
  216. dev = &fallback_dev;
  217. phys_mem = virt_to_phys(addr);
  218. if (!need_iommu(dev, phys_mem, size))
  219. return phys_mem;
  220. bus = gart_map_simple(dev, addr, size, dir);
  221. return bus;
  222. }
  223. /*
  224. * Free a DMA mapping.
  225. */
  226. static void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
  227. size_t size, int direction)
  228. {
  229. unsigned long iommu_page;
  230. int npages;
  231. int i;
  232. if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
  233. dma_addr >= iommu_bus_base + iommu_size)
  234. return;
  235. iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
  236. npages = to_pages(dma_addr, size);
  237. for (i = 0; i < npages; i++) {
  238. iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
  239. CLEAR_LEAK(iommu_page + i);
  240. }
  241. free_iommu(iommu_page, npages);
  242. }
  243. /*
  244. * Wrapper for pci_unmap_single working with scatterlists.
  245. */
  246. static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
  247. {
  248. struct scatterlist *s;
  249. int i;
  250. for_each_sg(sg, s, nents, i) {
  251. if (!s->dma_length || !s->length)
  252. break;
  253. gart_unmap_single(dev, s->dma_address, s->dma_length, dir);
  254. }
  255. }
  256. /* Fallback for dma_map_sg in case of overflow */
  257. static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
  258. int nents, int dir)
  259. {
  260. struct scatterlist *s;
  261. int i;
  262. #ifdef CONFIG_IOMMU_DEBUG
  263. printk(KERN_DEBUG "dma_map_sg overflow\n");
  264. #endif
  265. for_each_sg(sg, s, nents, i) {
  266. unsigned long addr = page_to_phys(s->page) + s->offset;
  267. if (nonforced_iommu(dev, addr, s->length)) {
  268. addr = dma_map_area(dev, addr, s->length, dir);
  269. if (addr == bad_dma_address) {
  270. if (i > 0)
  271. gart_unmap_sg(dev, sg, i, dir);
  272. nents = 0;
  273. sg[0].dma_length = 0;
  274. break;
  275. }
  276. }
  277. s->dma_address = addr;
  278. s->dma_length = s->length;
  279. }
  280. flush_gart();
  281. return nents;
  282. }
  283. /* Map multiple scatterlist entries continuous into the first. */
  284. static int __dma_map_cont(struct scatterlist *start, int nelems,
  285. struct scatterlist *sout, unsigned long pages)
  286. {
  287. unsigned long iommu_start = alloc_iommu(pages);
  288. unsigned long iommu_page = iommu_start;
  289. struct scatterlist *s;
  290. int i;
  291. if (iommu_start == -1)
  292. return -1;
  293. for_each_sg(start, s, nelems, i) {
  294. unsigned long pages, addr;
  295. unsigned long phys_addr = s->dma_address;
  296. BUG_ON(s != start && s->offset);
  297. if (s == start) {
  298. *sout = *s;
  299. sout->dma_address = iommu_bus_base;
  300. sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
  301. sout->dma_length = s->length;
  302. } else {
  303. sout->dma_length += s->length;
  304. }
  305. addr = phys_addr;
  306. pages = to_pages(s->offset, s->length);
  307. while (pages--) {
  308. iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
  309. SET_LEAK(iommu_page);
  310. addr += PAGE_SIZE;
  311. iommu_page++;
  312. }
  313. }
  314. BUG_ON(iommu_page - iommu_start != pages);
  315. return 0;
  316. }
  317. static inline int dma_map_cont(struct scatterlist *start, int nelems,
  318. struct scatterlist *sout,
  319. unsigned long pages, int need)
  320. {
  321. if (!need) {
  322. BUG_ON(nelems != 1);
  323. *sout = *start;
  324. sout->dma_length = start->length;
  325. return 0;
  326. }
  327. return __dma_map_cont(start, nelems, sout, pages);
  328. }
  329. /*
  330. * DMA map all entries in a scatterlist.
  331. * Merge chunks that have page aligned sizes into a continuous mapping.
  332. */
  333. static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  334. int dir)
  335. {
  336. int i;
  337. int out;
  338. int start;
  339. unsigned long pages = 0;
  340. int need = 0, nextneed;
  341. struct scatterlist *s, *ps, *start_sg, *sgmap;
  342. if (nents == 0)
  343. return 0;
  344. if (!dev)
  345. dev = &fallback_dev;
  346. out = 0;
  347. start = 0;
  348. start_sg = sgmap = sg;
  349. ps = NULL; /* shut up gcc */
  350. for_each_sg(sg, s, nents, i) {
  351. dma_addr_t addr = page_to_phys(s->page) + s->offset;
  352. s->dma_address = addr;
  353. BUG_ON(s->length == 0);
  354. nextneed = need_iommu(dev, addr, s->length);
  355. /* Handle the previous not yet processed entries */
  356. if (i > start) {
  357. /* Can only merge when the last chunk ends on a page
  358. boundary and the new one doesn't have an offset. */
  359. if (!iommu_merge || !nextneed || !need || s->offset ||
  360. (ps->offset + ps->length) % PAGE_SIZE) {
  361. if (dma_map_cont(start_sg, i - start, sgmap,
  362. pages, need) < 0)
  363. goto error;
  364. out++;
  365. sgmap = sg_next(sgmap);
  366. pages = 0;
  367. start = i;
  368. start_sg = s;
  369. }
  370. }
  371. need = nextneed;
  372. pages += to_pages(s->offset, s->length);
  373. ps = s;
  374. }
  375. if (dma_map_cont(start_sg, i - start, sgmap, pages, need) < 0)
  376. goto error;
  377. out++;
  378. flush_gart();
  379. if (out < nents) {
  380. sgmap = sg_next(sgmap);
  381. sgmap->dma_length = 0;
  382. }
  383. return out;
  384. error:
  385. flush_gart();
  386. gart_unmap_sg(dev, sg, nents, dir);
  387. /* When it was forced or merged try again in a dumb way */
  388. if (force_iommu || iommu_merge) {
  389. out = dma_map_sg_nonforce(dev, sg, nents, dir);
  390. if (out > 0)
  391. return out;
  392. }
  393. if (panic_on_overflow)
  394. panic("dma_map_sg: overflow on %lu pages\n", pages);
  395. iommu_full(dev, pages << PAGE_SHIFT, dir);
  396. for_each_sg(sg, s, nents, i)
  397. s->dma_address = bad_dma_address;
  398. return 0;
  399. }
  400. static int no_agp;
  401. static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
  402. {
  403. unsigned long a;
  404. if (!iommu_size) {
  405. iommu_size = aper_size;
  406. if (!no_agp)
  407. iommu_size /= 2;
  408. }
  409. a = aper + iommu_size;
  410. iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
  411. if (iommu_size < 64*1024*1024)
  412. printk(KERN_WARNING
  413. "PCI-DMA: Warning: Small IOMMU %luMB. Consider increasing the AGP aperture in BIOS\n",iommu_size>>20);
  414. return iommu_size;
  415. }
  416. static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
  417. {
  418. unsigned aper_size = 0, aper_base_32;
  419. u64 aper_base;
  420. unsigned aper_order;
  421. pci_read_config_dword(dev, 0x94, &aper_base_32);
  422. pci_read_config_dword(dev, 0x90, &aper_order);
  423. aper_order = (aper_order >> 1) & 7;
  424. aper_base = aper_base_32 & 0x7fff;
  425. aper_base <<= 25;
  426. aper_size = (32 * 1024 * 1024) << aper_order;
  427. if (aper_base + aper_size > 0x100000000UL || !aper_size)
  428. aper_base = 0;
  429. *size = aper_size;
  430. return aper_base;
  431. }
  432. /*
  433. * Private Northbridge GATT initialization in case we cannot use the
  434. * AGP driver for some reason.
  435. */
  436. static __init int init_k8_gatt(struct agp_kern_info *info)
  437. {
  438. struct pci_dev *dev;
  439. void *gatt;
  440. unsigned aper_base, new_aper_base;
  441. unsigned aper_size, gatt_size, new_aper_size;
  442. int i;
  443. printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
  444. aper_size = aper_base = info->aper_size = 0;
  445. dev = NULL;
  446. for (i = 0; i < num_k8_northbridges; i++) {
  447. dev = k8_northbridges[i];
  448. new_aper_base = read_aperture(dev, &new_aper_size);
  449. if (!new_aper_base)
  450. goto nommu;
  451. if (!aper_base) {
  452. aper_size = new_aper_size;
  453. aper_base = new_aper_base;
  454. }
  455. if (aper_size != new_aper_size || aper_base != new_aper_base)
  456. goto nommu;
  457. }
  458. if (!aper_base)
  459. goto nommu;
  460. info->aper_base = aper_base;
  461. info->aper_size = aper_size>>20;
  462. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
  463. gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size));
  464. if (!gatt)
  465. panic("Cannot allocate GATT table");
  466. if (change_page_attr_addr((unsigned long)gatt, gatt_size >> PAGE_SHIFT, PAGE_KERNEL_NOCACHE))
  467. panic("Could not set GART PTEs to uncacheable pages");
  468. global_flush_tlb();
  469. memset(gatt, 0, gatt_size);
  470. agp_gatt_table = gatt;
  471. for (i = 0; i < num_k8_northbridges; i++) {
  472. u32 ctl;
  473. u32 gatt_reg;
  474. dev = k8_northbridges[i];
  475. gatt_reg = __pa(gatt) >> 12;
  476. gatt_reg <<= 4;
  477. pci_write_config_dword(dev, 0x98, gatt_reg);
  478. pci_read_config_dword(dev, 0x90, &ctl);
  479. ctl |= 1;
  480. ctl &= ~((1<<4) | (1<<5));
  481. pci_write_config_dword(dev, 0x90, ctl);
  482. }
  483. flush_gart();
  484. printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10);
  485. return 0;
  486. nommu:
  487. /* Should not happen anymore */
  488. printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
  489. KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction.\n");
  490. return -1;
  491. }
  492. extern int agp_amd64_init(void);
  493. static const struct dma_mapping_ops gart_dma_ops = {
  494. .mapping_error = NULL,
  495. .map_single = gart_map_single,
  496. .map_simple = gart_map_simple,
  497. .unmap_single = gart_unmap_single,
  498. .sync_single_for_cpu = NULL,
  499. .sync_single_for_device = NULL,
  500. .sync_single_range_for_cpu = NULL,
  501. .sync_single_range_for_device = NULL,
  502. .sync_sg_for_cpu = NULL,
  503. .sync_sg_for_device = NULL,
  504. .map_sg = gart_map_sg,
  505. .unmap_sg = gart_unmap_sg,
  506. };
  507. void gart_iommu_shutdown(void)
  508. {
  509. struct pci_dev *dev;
  510. int i;
  511. if (no_agp && (dma_ops != &gart_dma_ops))
  512. return;
  513. for (i = 0; i < num_k8_northbridges; i++) {
  514. u32 ctl;
  515. dev = k8_northbridges[i];
  516. pci_read_config_dword(dev, 0x90, &ctl);
  517. ctl &= ~1;
  518. pci_write_config_dword(dev, 0x90, ctl);
  519. }
  520. }
  521. void __init gart_iommu_init(void)
  522. {
  523. struct agp_kern_info info;
  524. unsigned long aper_size;
  525. unsigned long iommu_start;
  526. unsigned long scratch;
  527. long i;
  528. if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) {
  529. printk(KERN_INFO "PCI-GART: No AMD northbridge found.\n");
  530. return;
  531. }
  532. #ifndef CONFIG_AGP_AMD64
  533. no_agp = 1;
  534. #else
  535. /* Makefile puts PCI initialization via subsys_initcall first. */
  536. /* Add other K8 AGP bridge drivers here */
  537. no_agp = no_agp ||
  538. (agp_amd64_init() < 0) ||
  539. (agp_copy_info(agp_bridge, &info) < 0);
  540. #endif
  541. if (swiotlb)
  542. return;
  543. /* Did we detect a different HW IOMMU? */
  544. if (iommu_detected && !iommu_aperture)
  545. return;
  546. if (no_iommu ||
  547. (!force_iommu && end_pfn <= MAX_DMA32_PFN) ||
  548. !iommu_aperture ||
  549. (no_agp && init_k8_gatt(&info) < 0)) {
  550. if (end_pfn > MAX_DMA32_PFN) {
  551. printk(KERN_ERR "WARNING more than 4GB of memory "
  552. "but GART IOMMU not available.\n"
  553. KERN_ERR "WARNING 32bit PCI may malfunction.\n");
  554. }
  555. return;
  556. }
  557. printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
  558. aper_size = info.aper_size * 1024 * 1024;
  559. iommu_size = check_iommu_size(info.aper_base, aper_size);
  560. iommu_pages = iommu_size >> PAGE_SHIFT;
  561. iommu_gart_bitmap = (void*)__get_free_pages(GFP_KERNEL,
  562. get_order(iommu_pages/8));
  563. if (!iommu_gart_bitmap)
  564. panic("Cannot allocate iommu bitmap\n");
  565. memset(iommu_gart_bitmap, 0, iommu_pages/8);
  566. #ifdef CONFIG_IOMMU_LEAK
  567. if (leak_trace) {
  568. iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL,
  569. get_order(iommu_pages*sizeof(void *)));
  570. if (iommu_leak_tab)
  571. memset(iommu_leak_tab, 0, iommu_pages * 8);
  572. else
  573. printk("PCI-DMA: Cannot allocate leak trace area\n");
  574. }
  575. #endif
  576. /*
  577. * Out of IOMMU space handling.
  578. * Reserve some invalid pages at the beginning of the GART.
  579. */
  580. set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
  581. agp_memory_reserved = iommu_size;
  582. printk(KERN_INFO
  583. "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
  584. iommu_size>>20);
  585. iommu_start = aper_size - iommu_size;
  586. iommu_bus_base = info.aper_base + iommu_start;
  587. bad_dma_address = iommu_bus_base;
  588. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  589. /*
  590. * Unmap the IOMMU part of the GART. The alias of the page is
  591. * always mapped with cache enabled and there is no full cache
  592. * coherency across the GART remapping. The unmapping avoids
  593. * automatic prefetches from the CPU allocating cache lines in
  594. * there. All CPU accesses are done via the direct mapping to
  595. * the backing memory. The GART address is only used by PCI
  596. * devices.
  597. */
  598. clear_kernel_mapping((unsigned long)__va(iommu_bus_base), iommu_size);
  599. /*
  600. * Try to workaround a bug (thanks to BenH)
  601. * Set unmapped entries to a scratch page instead of 0.
  602. * Any prefetches that hit unmapped entries won't get an bus abort
  603. * then.
  604. */
  605. scratch = get_zeroed_page(GFP_KERNEL);
  606. if (!scratch)
  607. panic("Cannot allocate iommu scratch page");
  608. gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
  609. for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
  610. iommu_gatt_base[i] = gart_unmapped_entry;
  611. flush_gart();
  612. dma_ops = &gart_dma_ops;
  613. }
  614. void __init gart_parse_options(char *p)
  615. {
  616. int arg;
  617. #ifdef CONFIG_IOMMU_LEAK
  618. if (!strncmp(p,"leak",4)) {
  619. leak_trace = 1;
  620. p += 4;
  621. if (*p == '=') ++p;
  622. if (isdigit(*p) && get_option(&p, &arg))
  623. iommu_leak_pages = arg;
  624. }
  625. #endif
  626. if (isdigit(*p) && get_option(&p, &arg))
  627. iommu_size = arg;
  628. if (!strncmp(p, "fullflush",8))
  629. iommu_fullflush = 1;
  630. if (!strncmp(p, "nofullflush",11))
  631. iommu_fullflush = 0;
  632. if (!strncmp(p,"noagp",5))
  633. no_agp = 1;
  634. if (!strncmp(p, "noaperture",10))
  635. fix_aperture = 0;
  636. /* duplicated from pci-dma.c */
  637. if (!strncmp(p,"force",5))
  638. iommu_aperture_allowed = 1;
  639. if (!strncmp(p,"allowed",7))
  640. iommu_aperture_allowed = 1;
  641. if (!strncmp(p, "memaper", 7)) {
  642. fallback_aper_force = 1;
  643. p += 7;
  644. if (*p == '=') {
  645. ++p;
  646. if (get_option(&p, &arg))
  647. fallback_aper_order = arg;
  648. }
  649. }
  650. }