amd_gart_64.c 22 KB

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