pci-gart_64.c 22 KB

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