pci-gart_64.c 22 KB

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