pci-gart_64.c 19 KB

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