pci-gart_64.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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/PCI/PCI-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 <linux/io.h>
  30. #include <asm/atomic.h>
  31. #include <asm/mtrr.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/proto.h>
  34. #include <asm/iommu.h>
  35. #include <asm/gart.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/swiotlb.h>
  38. #include <asm/dma.h>
  39. #include <asm/k8.h>
  40. static unsigned long iommu_bus_base; /* GART remapping area (physical) */
  41. static unsigned long iommu_size; /* size of remapping area bytes */
  42. static unsigned long iommu_pages; /* .. and in pages */
  43. static u32 *iommu_gatt_base; /* Remapping table */
  44. /*
  45. * If this is disabled the IOMMU will use an optimized flushing strategy
  46. * of only flushing when an mapping is reused. With it true the GART is
  47. * flushed for every mapping. Problem is that doing the lazy flush seems
  48. * to trigger bugs with some popular PCI cards, in particular 3ware (but
  49. * has been also also seen with Qlogic at least).
  50. */
  51. static int iommu_fullflush = 1;
  52. /* Allocation bitmap for the remapping area: */
  53. static DEFINE_SPINLOCK(iommu_bitmap_lock);
  54. /* Guarded by iommu_bitmap_lock: */
  55. static unsigned long *iommu_gart_bitmap;
  56. static u32 gart_unmapped_entry;
  57. #define GPTE_VALID 1
  58. #define GPTE_COHERENT 2
  59. #define GPTE_ENCODE(x) \
  60. (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
  61. #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
  62. #define EMERGENCY_PAGES 32 /* = 128KB */
  63. #ifdef CONFIG_AGP
  64. #define AGPEXTERN extern
  65. #else
  66. #define AGPEXTERN
  67. #endif
  68. /* backdoor interface to AGP driver */
  69. AGPEXTERN int agp_memory_reserved;
  70. AGPEXTERN __u32 *agp_gatt_table;
  71. static unsigned long next_bit; /* protected by iommu_bitmap_lock */
  72. static bool need_flush; /* global flush state. set for each gart wrap */
  73. static unsigned long alloc_iommu(struct device *dev, int size,
  74. unsigned long align_mask)
  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((unsigned long long)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, align_mask);
  86. if (offset == -1) {
  87. need_flush = true;
  88. offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
  89. size, base_index, boundary_size,
  90. align_mask);
  91. }
  92. if (offset != -1) {
  93. next_bit = offset+size;
  94. if (next_bit >= iommu_pages) {
  95. next_bit = 0;
  96. need_flush = true;
  97. }
  98. }
  99. if (iommu_fullflush)
  100. need_flush = true;
  101. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  102. return offset;
  103. }
  104. static void free_iommu(unsigned long offset, int size)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  108. iommu_area_free(iommu_gart_bitmap, offset, size);
  109. if (offset >= next_bit)
  110. next_bit = offset + size;
  111. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  112. }
  113. /*
  114. * Use global flush state to avoid races with multiple flushers.
  115. */
  116. static void flush_gart(void)
  117. {
  118. unsigned long flags;
  119. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  120. if (need_flush) {
  121. k8_flush_garts();
  122. need_flush = false;
  123. }
  124. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  125. }
  126. #ifdef CONFIG_IOMMU_LEAK
  127. /* Debugging aid for drivers that don't free their IOMMU tables */
  128. static int leak_trace;
  129. static int iommu_leak_pages = 20;
  130. static void dump_leak(void)
  131. {
  132. static int dump;
  133. if (dump)
  134. return;
  135. dump = 1;
  136. show_stack(NULL, NULL);
  137. debug_dma_dump_mappings(NULL);
  138. }
  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. dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
  152. if (size > PAGE_SIZE*EMERGENCY_PAGES) {
  153. if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  154. panic("PCI-DMA: Memory would be corrupted\n");
  155. if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  156. panic(KERN_ERR
  157. "PCI-DMA: Random memory would be DMAed\n");
  158. }
  159. #ifdef CONFIG_IOMMU_LEAK
  160. dump_leak();
  161. #endif
  162. }
  163. static inline int
  164. need_iommu(struct device *dev, unsigned long addr, size_t size)
  165. {
  166. return force_iommu ||
  167. !is_buffer_dma_capable(*dev->dma_mask, addr, size);
  168. }
  169. static inline int
  170. nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
  171. {
  172. return !is_buffer_dma_capable(*dev->dma_mask, addr, size);
  173. }
  174. /* Map a single continuous physical area into the IOMMU.
  175. * Caller needs to check if the iommu is needed and flush.
  176. */
  177. static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
  178. size_t size, int dir, unsigned long align_mask)
  179. {
  180. unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
  181. unsigned long iommu_page = alloc_iommu(dev, npages, align_mask);
  182. int i;
  183. if (iommu_page == -1) {
  184. if (!nonforced_iommu(dev, phys_mem, size))
  185. return phys_mem;
  186. if (panic_on_overflow)
  187. panic("dma_map_area overflow %lu bytes\n", size);
  188. iommu_full(dev, size, dir);
  189. return bad_dma_address;
  190. }
  191. for (i = 0; i < npages; i++) {
  192. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
  193. phys_mem += PAGE_SIZE;
  194. }
  195. return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
  196. }
  197. /* Map a single area into the IOMMU */
  198. static dma_addr_t gart_map_page(struct device *dev, struct page *page,
  199. unsigned long offset, size_t size,
  200. enum dma_data_direction dir,
  201. struct dma_attrs *attrs)
  202. {
  203. unsigned long bus;
  204. phys_addr_t paddr = page_to_phys(page) + offset;
  205. if (!dev)
  206. dev = &x86_dma_fallback_dev;
  207. if (!need_iommu(dev, paddr, size))
  208. return paddr;
  209. bus = dma_map_area(dev, paddr, size, dir, 0);
  210. flush_gart();
  211. return bus;
  212. }
  213. /*
  214. * Free a DMA mapping.
  215. */
  216. static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
  217. size_t size, enum dma_data_direction dir,
  218. struct dma_attrs *attrs)
  219. {
  220. unsigned long iommu_page;
  221. int npages;
  222. int i;
  223. if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
  224. dma_addr >= iommu_bus_base + iommu_size)
  225. return;
  226. iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
  227. npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  228. for (i = 0; i < npages; i++) {
  229. iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
  230. }
  231. free_iommu(iommu_page, npages);
  232. }
  233. /*
  234. * Wrapper for pci_unmap_single working with scatterlists.
  235. */
  236. static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  237. enum dma_data_direction dir, struct dma_attrs *attrs)
  238. {
  239. struct scatterlist *s;
  240. int i;
  241. for_each_sg(sg, s, nents, i) {
  242. if (!s->dma_length || !s->length)
  243. break;
  244. gart_unmap_page(dev, s->dma_address, s->dma_length, dir, NULL);
  245. }
  246. }
  247. /* Fallback for dma_map_sg in case of overflow */
  248. static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
  249. int nents, int dir)
  250. {
  251. struct scatterlist *s;
  252. int i;
  253. #ifdef CONFIG_IOMMU_DEBUG
  254. printk(KERN_DEBUG "dma_map_sg overflow\n");
  255. #endif
  256. for_each_sg(sg, s, nents, i) {
  257. unsigned long addr = sg_phys(s);
  258. if (nonforced_iommu(dev, addr, s->length)) {
  259. addr = dma_map_area(dev, addr, s->length, dir, 0);
  260. if (addr == bad_dma_address) {
  261. if (i > 0)
  262. gart_unmap_sg(dev, sg, i, dir, NULL);
  263. nents = 0;
  264. sg[0].dma_length = 0;
  265. break;
  266. }
  267. }
  268. s->dma_address = addr;
  269. s->dma_length = s->length;
  270. }
  271. flush_gart();
  272. return nents;
  273. }
  274. /* Map multiple scatterlist entries continuous into the first. */
  275. static int __dma_map_cont(struct device *dev, struct scatterlist *start,
  276. int nelems, struct scatterlist *sout,
  277. unsigned long pages)
  278. {
  279. unsigned long iommu_start = alloc_iommu(dev, pages, 0);
  280. unsigned long iommu_page = iommu_start;
  281. struct scatterlist *s;
  282. int i;
  283. if (iommu_start == -1)
  284. return -1;
  285. for_each_sg(start, s, nelems, i) {
  286. unsigned long pages, addr;
  287. unsigned long phys_addr = s->dma_address;
  288. BUG_ON(s != start && s->offset);
  289. if (s == start) {
  290. sout->dma_address = iommu_bus_base;
  291. sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
  292. sout->dma_length = s->length;
  293. } else {
  294. sout->dma_length += s->length;
  295. }
  296. addr = phys_addr;
  297. pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE);
  298. while (pages--) {
  299. iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
  300. addr += PAGE_SIZE;
  301. iommu_page++;
  302. }
  303. }
  304. BUG_ON(iommu_page - iommu_start != pages);
  305. return 0;
  306. }
  307. static inline int
  308. dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
  309. struct scatterlist *sout, unsigned long pages, int need)
  310. {
  311. if (!need) {
  312. BUG_ON(nelems != 1);
  313. sout->dma_address = start->dma_address;
  314. sout->dma_length = start->length;
  315. return 0;
  316. }
  317. return __dma_map_cont(dev, start, nelems, sout, pages);
  318. }
  319. /*
  320. * DMA map all entries in a scatterlist.
  321. * Merge chunks that have page aligned sizes into a continuous mapping.
  322. */
  323. static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  324. enum dma_data_direction dir, struct dma_attrs *attrs)
  325. {
  326. struct scatterlist *s, *ps, *start_sg, *sgmap;
  327. int need = 0, nextneed, i, out, start;
  328. unsigned long pages = 0;
  329. unsigned int seg_size;
  330. unsigned int max_seg_size;
  331. if (nents == 0)
  332. return 0;
  333. if (!dev)
  334. dev = &x86_dma_fallback_dev;
  335. out = 0;
  336. start = 0;
  337. start_sg = sgmap = sg;
  338. seg_size = 0;
  339. max_seg_size = dma_get_max_seg_size(dev);
  340. ps = NULL; /* shut up gcc */
  341. for_each_sg(sg, s, nents, i) {
  342. dma_addr_t addr = sg_phys(s);
  343. s->dma_address = addr;
  344. BUG_ON(s->length == 0);
  345. nextneed = need_iommu(dev, addr, s->length);
  346. /* Handle the previous not yet processed entries */
  347. if (i > start) {
  348. /*
  349. * Can only merge when the last chunk ends on a
  350. * page boundary and the new one doesn't have an
  351. * offset.
  352. */
  353. if (!iommu_merge || !nextneed || !need || s->offset ||
  354. (s->length + seg_size > max_seg_size) ||
  355. (ps->offset + ps->length) % PAGE_SIZE) {
  356. if (dma_map_cont(dev, start_sg, i - start,
  357. sgmap, pages, need) < 0)
  358. goto error;
  359. out++;
  360. seg_size = 0;
  361. sgmap = sg_next(sgmap);
  362. pages = 0;
  363. start = i;
  364. start_sg = s;
  365. }
  366. }
  367. seg_size += s->length;
  368. need = nextneed;
  369. pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
  370. ps = s;
  371. }
  372. if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
  373. goto error;
  374. out++;
  375. flush_gart();
  376. if (out < nents) {
  377. sgmap = sg_next(sgmap);
  378. sgmap->dma_length = 0;
  379. }
  380. return out;
  381. error:
  382. flush_gart();
  383. gart_unmap_sg(dev, sg, out, dir, NULL);
  384. /* When it was forced or merged try again in a dumb way */
  385. if (force_iommu || iommu_merge) {
  386. out = dma_map_sg_nonforce(dev, sg, nents, dir);
  387. if (out > 0)
  388. return out;
  389. }
  390. if (panic_on_overflow)
  391. panic("dma_map_sg: overflow on %lu pages\n", pages);
  392. iommu_full(dev, pages << PAGE_SHIFT, dir);
  393. for_each_sg(sg, s, nents, i)
  394. s->dma_address = bad_dma_address;
  395. return 0;
  396. }
  397. /* allocate and map a coherent mapping */
  398. static void *
  399. gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
  400. gfp_t flag)
  401. {
  402. dma_addr_t paddr;
  403. unsigned long align_mask;
  404. struct page *page;
  405. if (force_iommu && !(flag & GFP_DMA)) {
  406. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  407. page = alloc_pages(flag | __GFP_ZERO, get_order(size));
  408. if (!page)
  409. return NULL;
  410. align_mask = (1UL << get_order(size)) - 1;
  411. paddr = dma_map_area(dev, page_to_phys(page), size,
  412. DMA_BIDIRECTIONAL, align_mask);
  413. flush_gart();
  414. if (paddr != bad_dma_address) {
  415. *dma_addr = paddr;
  416. return page_address(page);
  417. }
  418. __free_pages(page, get_order(size));
  419. } else
  420. return dma_generic_alloc_coherent(dev, size, dma_addr, flag);
  421. return NULL;
  422. }
  423. /* free a coherent mapping */
  424. static void
  425. gart_free_coherent(struct device *dev, size_t size, void *vaddr,
  426. dma_addr_t dma_addr)
  427. {
  428. gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL);
  429. free_pages((unsigned long)vaddr, get_order(size));
  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. printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
  532. aper_size = aper_base = info->aper_size = 0;
  533. dev = NULL;
  534. for (i = 0; i < num_k8_northbridges; i++) {
  535. dev = k8_northbridges[i];
  536. new_aper_base = read_aperture(dev, &new_aper_size);
  537. if (!new_aper_base)
  538. goto nommu;
  539. if (!aper_base) {
  540. aper_size = new_aper_size;
  541. aper_base = new_aper_base;
  542. }
  543. if (aper_size != new_aper_size || aper_base != new_aper_base)
  544. goto nommu;
  545. }
  546. if (!aper_base)
  547. goto nommu;
  548. info->aper_base = aper_base;
  549. info->aper_size = aper_size >> 20;
  550. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
  551. gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  552. 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. agp_gatt_table = gatt;
  558. error = sysdev_class_register(&gart_sysdev_class);
  559. if (!error)
  560. error = sysdev_register(&device_gart);
  561. if (error)
  562. panic("Could not register gart_sysdev -- "
  563. "would corrupt data on next suspend");
  564. flush_gart();
  565. printk(KERN_INFO "PCI-DMA: aperture base @ %x size %u KB\n",
  566. aper_base, aper_size>>10);
  567. return 0;
  568. nommu:
  569. /* Should not happen anymore */
  570. printk(KERN_WARNING "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
  571. "falling back to iommu=soft.\n");
  572. return -1;
  573. }
  574. static struct dma_map_ops gart_dma_ops = {
  575. .map_sg = gart_map_sg,
  576. .unmap_sg = gart_unmap_sg,
  577. .map_page = gart_map_page,
  578. .unmap_page = gart_unmap_page,
  579. .alloc_coherent = gart_alloc_coherent,
  580. .free_coherent = gart_free_coherent,
  581. };
  582. void gart_iommu_shutdown(void)
  583. {
  584. struct pci_dev *dev;
  585. int i;
  586. if (no_agp && (dma_ops != &gart_dma_ops))
  587. return;
  588. for (i = 0; i < num_k8_northbridges; i++) {
  589. u32 ctl;
  590. dev = k8_northbridges[i];
  591. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
  592. ctl &= ~GARTEN;
  593. pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
  594. }
  595. }
  596. void __init gart_iommu_init(void)
  597. {
  598. struct agp_kern_info info;
  599. unsigned long iommu_start;
  600. unsigned long aper_base, aper_size;
  601. unsigned long start_pfn, end_pfn;
  602. unsigned long scratch;
  603. long i;
  604. if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0)
  605. return;
  606. #ifndef CONFIG_AGP_AMD64
  607. no_agp = 1;
  608. #else
  609. /* Makefile puts PCI initialization via subsys_initcall first. */
  610. /* Add other K8 AGP bridge drivers here */
  611. no_agp = no_agp ||
  612. (agp_amd64_init() < 0) ||
  613. (agp_copy_info(agp_bridge, &info) < 0);
  614. #endif
  615. if (swiotlb)
  616. return;
  617. /* Did we detect a different HW IOMMU? */
  618. if (iommu_detected && !gart_iommu_aperture)
  619. return;
  620. if (no_iommu ||
  621. (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
  622. !gart_iommu_aperture ||
  623. (no_agp && init_k8_gatt(&info) < 0)) {
  624. if (max_pfn > MAX_DMA32_PFN) {
  625. printk(KERN_WARNING "More than 4GB of memory "
  626. "but GART IOMMU not available.\n");
  627. printk(KERN_WARNING "falling back to iommu=soft.\n");
  628. }
  629. return;
  630. }
  631. /* need to map that range */
  632. aper_size = info.aper_size << 20;
  633. aper_base = info.aper_base;
  634. end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
  635. if (end_pfn > max_low_pfn_mapped) {
  636. start_pfn = (aper_base>>PAGE_SHIFT);
  637. init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
  638. }
  639. printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
  640. iommu_size = check_iommu_size(info.aper_base, aper_size);
  641. iommu_pages = iommu_size >> PAGE_SHIFT;
  642. iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  643. get_order(iommu_pages/8));
  644. if (!iommu_gart_bitmap)
  645. panic("Cannot allocate iommu bitmap\n");
  646. #ifdef CONFIG_IOMMU_LEAK
  647. if (leak_trace) {
  648. int ret;
  649. ret = dma_debug_resize_entries(iommu_pages);
  650. if (ret)
  651. printk(KERN_DEBUG
  652. "PCI-DMA: Cannot trace all the entries\n");
  653. }
  654. #endif
  655. /*
  656. * Out of IOMMU space handling.
  657. * Reserve some invalid pages at the beginning of the GART.
  658. */
  659. iommu_area_reserve(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
  660. agp_memory_reserved = iommu_size;
  661. printk(KERN_INFO
  662. "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
  663. iommu_size >> 20);
  664. iommu_start = aper_size - iommu_size;
  665. iommu_bus_base = info.aper_base + iommu_start;
  666. bad_dma_address = iommu_bus_base;
  667. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  668. /*
  669. * Unmap the IOMMU part of the GART. The alias of the page is
  670. * always mapped with cache enabled and there is no full cache
  671. * coherency across the GART remapping. The unmapping avoids
  672. * automatic prefetches from the CPU allocating cache lines in
  673. * there. All CPU accesses are done via the direct mapping to
  674. * the backing memory. The GART address is only used by PCI
  675. * devices.
  676. */
  677. set_memory_np((unsigned long)__va(iommu_bus_base),
  678. iommu_size >> PAGE_SHIFT);
  679. /*
  680. * Tricky. The GART table remaps the physical memory range,
  681. * so the CPU wont notice potential aliases and if the memory
  682. * is remapped to UC later on, we might surprise the PCI devices
  683. * with a stray writeout of a cacheline. So play it sure and
  684. * do an explicit, full-scale wbinvd() _after_ having marked all
  685. * the pages as Not-Present:
  686. */
  687. wbinvd();
  688. /*
  689. * Now all caches are flushed and we can safely enable
  690. * GART hardware. Doing it early leaves the possibility
  691. * of stale cache entries that can lead to GART PTE
  692. * errors.
  693. */
  694. enable_gart_translations();
  695. /*
  696. * Try to workaround a bug (thanks to BenH):
  697. * Set unmapped entries to a scratch page instead of 0.
  698. * Any prefetches that hit unmapped entries won't get an bus abort
  699. * then. (P2P bridge may be prefetching on DMA reads).
  700. */
  701. scratch = get_zeroed_page(GFP_KERNEL);
  702. if (!scratch)
  703. panic("Cannot allocate iommu scratch page");
  704. gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
  705. for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
  706. iommu_gatt_base[i] = gart_unmapped_entry;
  707. flush_gart();
  708. dma_ops = &gart_dma_ops;
  709. }
  710. void __init gart_parse_options(char *p)
  711. {
  712. int arg;
  713. #ifdef CONFIG_IOMMU_LEAK
  714. if (!strncmp(p, "leak", 4)) {
  715. leak_trace = 1;
  716. p += 4;
  717. if (*p == '=')
  718. ++p;
  719. if (isdigit(*p) && get_option(&p, &arg))
  720. iommu_leak_pages = arg;
  721. }
  722. #endif
  723. if (isdigit(*p) && get_option(&p, &arg))
  724. iommu_size = arg;
  725. if (!strncmp(p, "fullflush", 8))
  726. iommu_fullflush = 1;
  727. if (!strncmp(p, "nofullflush", 11))
  728. iommu_fullflush = 0;
  729. if (!strncmp(p, "noagp", 5))
  730. no_agp = 1;
  731. if (!strncmp(p, "noaperture", 10))
  732. fix_aperture = 0;
  733. /* duplicated from pci-dma.c */
  734. if (!strncmp(p, "force", 5))
  735. gart_iommu_aperture_allowed = 1;
  736. if (!strncmp(p, "allowed", 7))
  737. gart_iommu_aperture_allowed = 1;
  738. if (!strncmp(p, "memaper", 7)) {
  739. fallback_aper_force = 1;
  740. p += 7;
  741. if (*p == '=') {
  742. ++p;
  743. if (get_option(&p, &arg))
  744. fallback_aper_order = arg;
  745. }
  746. }
  747. }