pci-gart_64.c 22 KB

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