pci-gart_64.c 22 KB

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