amd_gart_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Dynamic DMA mapping support for AMD Hammer.
  4. *
  5. * Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI.
  6. * This allows to use PCI devices that only support 32bit addresses on systems
  7. * with more than 4GB.
  8. *
  9. * See Documentation/core-api/dma-api-howto.rst for the interface specification.
  10. *
  11. * Copyright 2002 Andi Kleen, SuSE Labs.
  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/sched/debug.h>
  20. #include <linux/string.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pci.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/syscore_ops.h>
  30. #include <linux/io.h>
  31. #include <linux/gfp.h>
  32. #include <linux/atomic.h>
  33. #include <linux/dma-direct.h>
  34. #include <linux/dma-map-ops.h>
  35. #include <asm/mtrr.h>
  36. #include <asm/proto.h>
  37. #include <asm/iommu.h>
  38. #include <asm/gart.h>
  39. #include <asm/set_memory.h>
  40. #include <asm/dma.h>
  41. #include <asm/amd_nb.h>
  42. #include <asm/x86_init.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. /*
  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 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. #ifdef CONFIG_AGP
  66. #define AGPEXTERN extern
  67. #else
  68. #define AGPEXTERN
  69. #endif
  70. /* GART can only remap to physical addresses < 1TB */
  71. #define GART_MAX_PHYS_ADDR (1ULL << 40)
  72. /* backdoor interface to AGP driver */
  73. AGPEXTERN int agp_memory_reserved;
  74. AGPEXTERN __u32 *agp_gatt_table;
  75. static unsigned long next_bit; /* protected by iommu_bitmap_lock */
  76. static bool need_flush; /* global flush state. set for each gart wrap */
  77. static unsigned long alloc_iommu(struct device *dev, int size,
  78. unsigned long align_mask)
  79. {
  80. unsigned long offset, flags;
  81. unsigned long boundary_size;
  82. unsigned long base_index;
  83. base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
  84. PAGE_SIZE) >> PAGE_SHIFT;
  85. boundary_size = dma_get_seg_boundary_nr_pages(dev, 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. amd_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 void dump_leak(void)
  132. {
  133. static int dump;
  134. if (dump)
  135. return;
  136. dump = 1;
  137. show_stack(NULL, NULL, KERN_ERR);
  138. debug_dma_dump_mappings(NULL);
  139. }
  140. #endif
  141. static void iommu_full(struct device *dev, size_t size, int dir)
  142. {
  143. /*
  144. * Ran out of IOMMU space for this operation. This is very bad.
  145. * Unfortunately the drivers cannot handle this operation properly.
  146. * Return some non mapped prereserved space in the aperture and
  147. * let the Northbridge deal with it. This will result in garbage
  148. * in the IO operation. When the size exceeds the prereserved space
  149. * memory corruption will occur or random memory will be DMAed
  150. * out. Hopefully no network devices use single mappings that big.
  151. */
  152. dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
  153. #ifdef CONFIG_IOMMU_LEAK
  154. dump_leak();
  155. #endif
  156. }
  157. static inline int
  158. need_iommu(struct device *dev, unsigned long addr, size_t size)
  159. {
  160. return force_iommu || !dma_capable(dev, addr, size, true);
  161. }
  162. static inline int
  163. nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
  164. {
  165. return !dma_capable(dev, addr, size, true);
  166. }
  167. /* Map a single continuous physical area into the IOMMU.
  168. * Caller needs to check if the iommu is needed and flush.
  169. */
  170. static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
  171. size_t size, int dir, unsigned long align_mask)
  172. {
  173. unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
  174. unsigned long iommu_page;
  175. int i;
  176. if (unlikely(phys_mem + size > GART_MAX_PHYS_ADDR))
  177. return DMA_MAPPING_ERROR;
  178. iommu_page = alloc_iommu(dev, npages, align_mask);
  179. if (iommu_page == -1) {
  180. if (!nonforced_iommu(dev, phys_mem, size))
  181. return phys_mem;
  182. if (panic_on_overflow)
  183. panic("dma_map_area overflow %lu bytes\n", size);
  184. iommu_full(dev, size, dir);
  185. return DMA_MAPPING_ERROR;
  186. }
  187. for (i = 0; i < npages; i++) {
  188. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
  189. phys_mem += PAGE_SIZE;
  190. }
  191. return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
  192. }
  193. /* Map a single area into the IOMMU */
  194. static dma_addr_t gart_map_page(struct device *dev, struct page *page,
  195. unsigned long offset, size_t size,
  196. enum dma_data_direction dir,
  197. unsigned long attrs)
  198. {
  199. unsigned long bus;
  200. phys_addr_t paddr = page_to_phys(page) + offset;
  201. if (!need_iommu(dev, paddr, size))
  202. return paddr;
  203. bus = dma_map_area(dev, paddr, size, dir, 0);
  204. flush_gart();
  205. return bus;
  206. }
  207. /*
  208. * Free a DMA mapping.
  209. */
  210. static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
  211. size_t size, enum dma_data_direction dir,
  212. unsigned long attrs)
  213. {
  214. unsigned long iommu_page;
  215. int npages;
  216. int i;
  217. if (WARN_ON_ONCE(dma_addr == DMA_MAPPING_ERROR))
  218. return;
  219. /*
  220. * This driver will not always use a GART mapping, but might have
  221. * created a direct mapping instead. If that is the case there is
  222. * nothing to unmap here.
  223. */
  224. if (dma_addr < iommu_bus_base ||
  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, unsigned long 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, 0);
  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. pr_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 == DMA_MAPPING_ERROR) {
  262. if (i > 0)
  263. gart_unmap_sg(dev, sg, i, dir, 0);
  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 -ENOMEM;
  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, unsigned long attrs)
  326. {
  327. struct scatterlist *s, *ps, *start_sg, *sgmap;
  328. int need = 0, nextneed, i, out, start, ret;
  329. unsigned long pages = 0;
  330. unsigned int seg_size;
  331. unsigned int max_seg_size;
  332. if (nents == 0)
  333. return -EINVAL;
  334. out = 0;
  335. start = 0;
  336. start_sg = sg;
  337. sgmap = sg;
  338. seg_size = 0;
  339. max_seg_size = dma_get_max_seg_size(dev);
  340. ps = NULL; /* shut up gcc */
  341. for_each_sg(sg, s, nents, i) {
  342. dma_addr_t addr = sg_phys(s);
  343. s->dma_address = addr;
  344. BUG_ON(s->length == 0);
  345. nextneed = need_iommu(dev, addr, s->length);
  346. /* Handle the previous not yet processed entries */
  347. if (i > start) {
  348. /*
  349. * Can only merge when the last chunk ends on a
  350. * page boundary and the new one doesn't have an
  351. * offset.
  352. */
  353. if (!iommu_merge || !nextneed || !need || s->offset ||
  354. (s->length + seg_size > max_seg_size) ||
  355. (ps->offset + ps->length) % PAGE_SIZE) {
  356. ret = dma_map_cont(dev, start_sg, i - start,
  357. sgmap, pages, need);
  358. if (ret < 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. ret = dma_map_cont(dev, start_sg, i - start, sgmap, pages, need);
  374. if (ret < 0)
  375. goto error;
  376. out++;
  377. flush_gart();
  378. if (out < nents) {
  379. sgmap = sg_next(sgmap);
  380. sgmap->dma_length = 0;
  381. }
  382. return out;
  383. error:
  384. flush_gart();
  385. gart_unmap_sg(dev, sg, out, dir, 0);
  386. /* When it was forced or merged try again in a dumb way */
  387. if (force_iommu || iommu_merge) {
  388. out = dma_map_sg_nonforce(dev, sg, nents, dir);
  389. if (out > 0)
  390. return out;
  391. }
  392. if (panic_on_overflow)
  393. panic("dma_map_sg: overflow on %lu pages\n", pages);
  394. iommu_full(dev, pages << PAGE_SHIFT, dir);
  395. return ret;
  396. }
  397. /* allocate and map a coherent mapping */
  398. static void *
  399. gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
  400. gfp_t flag, unsigned long attrs)
  401. {
  402. void *vaddr;
  403. vaddr = dma_direct_alloc(dev, size, dma_addr, flag, attrs);
  404. if (!vaddr ||
  405. !force_iommu || dev->coherent_dma_mask <= DMA_BIT_MASK(24))
  406. return vaddr;
  407. *dma_addr = dma_map_area(dev, virt_to_phys(vaddr), size,
  408. DMA_BIDIRECTIONAL, (1UL << get_order(size)) - 1);
  409. flush_gart();
  410. if (unlikely(*dma_addr == DMA_MAPPING_ERROR))
  411. goto out_free;
  412. return vaddr;
  413. out_free:
  414. dma_direct_free(dev, size, vaddr, *dma_addr, attrs);
  415. return NULL;
  416. }
  417. /* free a coherent mapping */
  418. static void
  419. gart_free_coherent(struct device *dev, size_t size, void *vaddr,
  420. dma_addr_t dma_addr, unsigned long attrs)
  421. {
  422. gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0);
  423. dma_direct_free(dev, size, vaddr, dma_addr, attrs);
  424. }
  425. static int no_agp;
  426. static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
  427. {
  428. unsigned long a;
  429. if (!iommu_size) {
  430. iommu_size = aper_size;
  431. if (!no_agp)
  432. iommu_size /= 2;
  433. }
  434. a = aper + iommu_size;
  435. iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
  436. if (iommu_size < 64*1024*1024) {
  437. pr_warn("PCI-DMA: Warning: Small IOMMU %luMB."
  438. " Consider increasing the AGP aperture in BIOS\n",
  439. iommu_size >> 20);
  440. }
  441. return iommu_size;
  442. }
  443. static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
  444. {
  445. unsigned aper_size = 0, aper_base_32, aper_order;
  446. u64 aper_base;
  447. pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
  448. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
  449. aper_order = (aper_order >> 1) & 7;
  450. aper_base = aper_base_32 & 0x7fff;
  451. aper_base <<= 25;
  452. aper_size = (32 * 1024 * 1024) << aper_order;
  453. if (aper_base + aper_size > 0x100000000UL || !aper_size)
  454. aper_base = 0;
  455. *size = aper_size;
  456. return aper_base;
  457. }
  458. static void enable_gart_translations(void)
  459. {
  460. int i;
  461. if (!amd_nb_has_feature(AMD_NB_GART))
  462. return;
  463. for (i = 0; i < amd_nb_num(); i++) {
  464. struct pci_dev *dev = node_to_amd_nb(i)->misc;
  465. enable_gart_translation(dev, __pa(agp_gatt_table));
  466. }
  467. /* Flush the GART-TLB to remove stale entries */
  468. amd_flush_garts();
  469. }
  470. /*
  471. * If fix_up_north_bridges is set, the north bridges have to be fixed up on
  472. * resume in the same way as they are handled in gart_iommu_hole_init().
  473. */
  474. static bool fix_up_north_bridges;
  475. static u32 aperture_order;
  476. static u32 aperture_alloc;
  477. void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
  478. {
  479. fix_up_north_bridges = true;
  480. aperture_order = aper_order;
  481. aperture_alloc = aper_alloc;
  482. }
  483. static void gart_fixup_northbridges(void)
  484. {
  485. int i;
  486. if (!fix_up_north_bridges)
  487. return;
  488. if (!amd_nb_has_feature(AMD_NB_GART))
  489. return;
  490. pr_info("PCI-DMA: Restoring GART aperture settings\n");
  491. for (i = 0; i < amd_nb_num(); i++) {
  492. struct pci_dev *dev = node_to_amd_nb(i)->misc;
  493. /*
  494. * Don't enable translations just yet. That is the next
  495. * step. Restore the pre-suspend aperture settings.
  496. */
  497. gart_set_size_and_enable(dev, aperture_order);
  498. pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
  499. }
  500. }
  501. static void gart_resume(void)
  502. {
  503. pr_info("PCI-DMA: Resuming GART IOMMU\n");
  504. gart_fixup_northbridges();
  505. enable_gart_translations();
  506. }
  507. static struct syscore_ops gart_syscore_ops = {
  508. .resume = gart_resume,
  509. };
  510. /*
  511. * Private Northbridge GATT initialization in case we cannot use the
  512. * AGP driver for some reason.
  513. */
  514. static __init int init_amd_gatt(struct agp_kern_info *info)
  515. {
  516. unsigned aper_size, gatt_size, new_aper_size;
  517. unsigned aper_base, new_aper_base;
  518. struct pci_dev *dev;
  519. void *gatt;
  520. int i;
  521. pr_info("PCI-DMA: Disabling AGP.\n");
  522. aper_size = aper_base = info->aper_size = 0;
  523. dev = NULL;
  524. for (i = 0; i < amd_nb_num(); i++) {
  525. dev = node_to_amd_nb(i)->misc;
  526. new_aper_base = read_aperture(dev, &new_aper_size);
  527. if (!new_aper_base)
  528. goto nommu;
  529. if (!aper_base) {
  530. aper_size = new_aper_size;
  531. aper_base = new_aper_base;
  532. }
  533. if (aper_size != new_aper_size || aper_base != new_aper_base)
  534. goto nommu;
  535. }
  536. if (!aper_base)
  537. goto nommu;
  538. info->aper_base = aper_base;
  539. info->aper_size = aper_size >> 20;
  540. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
  541. gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  542. get_order(gatt_size));
  543. if (!gatt)
  544. panic("Cannot allocate GATT table");
  545. if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
  546. panic("Could not set GART PTEs to uncacheable pages");
  547. agp_gatt_table = gatt;
  548. register_syscore_ops(&gart_syscore_ops);
  549. flush_gart();
  550. pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
  551. aper_base, aper_size>>10);
  552. return 0;
  553. nommu:
  554. /* Should not happen anymore */
  555. pr_warn("PCI-DMA: More than 4GB of RAM and no IOMMU - falling back to iommu=soft.\n");
  556. return -1;
  557. }
  558. static const struct dma_map_ops gart_dma_ops = {
  559. .map_sg = gart_map_sg,
  560. .unmap_sg = gart_unmap_sg,
  561. .map_page = gart_map_page,
  562. .unmap_page = gart_unmap_page,
  563. .alloc = gart_alloc_coherent,
  564. .free = gart_free_coherent,
  565. .mmap = dma_common_mmap,
  566. .get_sgtable = dma_common_get_sgtable,
  567. .dma_supported = dma_direct_supported,
  568. .get_required_mask = dma_direct_get_required_mask,
  569. .alloc_pages = dma_direct_alloc_pages,
  570. .free_pages = dma_direct_free_pages,
  571. };
  572. static void gart_iommu_shutdown(void)
  573. {
  574. struct pci_dev *dev;
  575. int i;
  576. /* don't shutdown it if there is AGP installed */
  577. if (!no_agp)
  578. return;
  579. if (!amd_nb_has_feature(AMD_NB_GART))
  580. return;
  581. for (i = 0; i < amd_nb_num(); i++) {
  582. u32 ctl;
  583. dev = node_to_amd_nb(i)->misc;
  584. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
  585. ctl &= ~GARTEN;
  586. pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
  587. }
  588. }
  589. int __init gart_iommu_init(void)
  590. {
  591. struct agp_kern_info info;
  592. unsigned long iommu_start;
  593. unsigned long aper_base, aper_size;
  594. unsigned long start_pfn, end_pfn;
  595. unsigned long scratch;
  596. if (!amd_nb_has_feature(AMD_NB_GART))
  597. return 0;
  598. #ifndef CONFIG_AGP_AMD64
  599. no_agp = 1;
  600. #else
  601. /* Makefile puts PCI initialization via subsys_initcall first. */
  602. /* Add other AMD AGP bridge drivers here */
  603. no_agp = no_agp ||
  604. (agp_amd64_init() < 0) ||
  605. (agp_copy_info(agp_bridge, &info) < 0);
  606. #endif
  607. if (no_iommu ||
  608. (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
  609. !gart_iommu_aperture ||
  610. (no_agp && init_amd_gatt(&info) < 0)) {
  611. if (max_pfn > MAX_DMA32_PFN) {
  612. pr_warn("More than 4GB of memory but GART IOMMU not available.\n");
  613. pr_warn("falling back to iommu=soft.\n");
  614. }
  615. return 0;
  616. }
  617. /* need to map that range */
  618. aper_size = info.aper_size << 20;
  619. aper_base = info.aper_base;
  620. end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
  621. start_pfn = PFN_DOWN(aper_base);
  622. if (!pfn_range_is_mapped(start_pfn, end_pfn))
  623. init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT,
  624. PAGE_KERNEL);
  625. pr_info("PCI-DMA: using GART IOMMU.\n");
  626. iommu_size = check_iommu_size(info.aper_base, aper_size);
  627. iommu_pages = iommu_size >> PAGE_SHIFT;
  628. iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  629. get_order(iommu_pages/8));
  630. if (!iommu_gart_bitmap)
  631. panic("Cannot allocate iommu bitmap\n");
  632. pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
  633. iommu_size >> 20);
  634. agp_memory_reserved = iommu_size;
  635. iommu_start = aper_size - iommu_size;
  636. iommu_bus_base = info.aper_base + iommu_start;
  637. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  638. /*
  639. * Unmap the IOMMU part of the GART. The alias of the page is
  640. * always mapped with cache enabled and there is no full cache
  641. * coherency across the GART remapping. The unmapping avoids
  642. * automatic prefetches from the CPU allocating cache lines in
  643. * there. All CPU accesses are done via the direct mapping to
  644. * the backing memory. The GART address is only used by PCI
  645. * devices.
  646. */
  647. set_memory_np((unsigned long)__va(iommu_bus_base),
  648. iommu_size >> PAGE_SHIFT);
  649. /*
  650. * Tricky. The GART table remaps the physical memory range,
  651. * so the CPU wont notice potential aliases and if the memory
  652. * is remapped to UC later on, we might surprise the PCI devices
  653. * with a stray writeout of a cacheline. So play it sure and
  654. * do an explicit, full-scale wbinvd() _after_ having marked all
  655. * the pages as Not-Present:
  656. */
  657. wbinvd();
  658. /*
  659. * Now all caches are flushed and we can safely enable
  660. * GART hardware. Doing it early leaves the possibility
  661. * of stale cache entries that can lead to GART PTE
  662. * errors.
  663. */
  664. enable_gart_translations();
  665. /*
  666. * Try to workaround a bug (thanks to BenH):
  667. * Set unmapped entries to a scratch page instead of 0.
  668. * Any prefetches that hit unmapped entries won't get an bus abort
  669. * then. (P2P bridge may be prefetching on DMA reads).
  670. */
  671. scratch = get_zeroed_page(GFP_KERNEL);
  672. if (!scratch)
  673. panic("Cannot allocate iommu scratch page");
  674. gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
  675. flush_gart();
  676. dma_ops = &gart_dma_ops;
  677. x86_platform.iommu_shutdown = gart_iommu_shutdown;
  678. x86_swiotlb_enable = false;
  679. return 0;
  680. }
  681. void __init gart_parse_options(char *p)
  682. {
  683. int arg;
  684. if (isdigit(*p) && get_option(&p, &arg))
  685. iommu_size = arg;
  686. if (!strncmp(p, "fullflush", 9))
  687. iommu_fullflush = 1;
  688. if (!strncmp(p, "nofullflush", 11))
  689. iommu_fullflush = 0;
  690. if (!strncmp(p, "noagp", 5))
  691. no_agp = 1;
  692. if (!strncmp(p, "noaperture", 10))
  693. fix_aperture = 0;
  694. /* duplicated from pci-dma.c */
  695. if (!strncmp(p, "force", 5))
  696. gart_iommu_aperture_allowed = 1;
  697. if (!strncmp(p, "allowed", 7))
  698. gart_iommu_aperture_allowed = 1;
  699. if (!strncmp(p, "memaper", 7)) {
  700. fallback_aper_force = 1;
  701. p += 7;
  702. if (*p == '=') {
  703. ++p;
  704. if (get_option(&p, &arg))
  705. fallback_aper_order = arg;
  706. }
  707. }
  708. }