amd_iommu.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/gfp.h>
  21. #include <linux/bitops.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/iommu-helper.h>
  24. #include <asm/proto.h>
  25. #include <asm/iommu.h>
  26. #include <asm/amd_iommu_types.h>
  27. #include <asm/amd_iommu.h>
  28. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  29. #define EXIT_LOOP_COUNT 10000000
  30. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  31. /*
  32. * general struct to manage commands send to an IOMMU
  33. */
  34. struct iommu_cmd {
  35. u32 data[4];
  36. };
  37. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  38. struct unity_map_entry *e);
  39. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  40. static int iommu_has_npcache(struct amd_iommu *iommu)
  41. {
  42. return iommu->cap & IOMMU_CAP_NPCACHE;
  43. }
  44. /****************************************************************************
  45. *
  46. * IOMMU command queuing functions
  47. *
  48. ****************************************************************************/
  49. /*
  50. * Writes the command to the IOMMUs command buffer and informs the
  51. * hardware about the new command. Must be called with iommu->lock held.
  52. */
  53. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  54. {
  55. u32 tail, head;
  56. u8 *target;
  57. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  58. target = (iommu->cmd_buf + tail);
  59. memcpy_toio(target, cmd, sizeof(*cmd));
  60. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  61. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  62. if (tail == head)
  63. return -ENOMEM;
  64. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  65. return 0;
  66. }
  67. /*
  68. * General queuing function for commands. Takes iommu->lock and calls
  69. * __iommu_queue_command().
  70. */
  71. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  72. {
  73. unsigned long flags;
  74. int ret;
  75. spin_lock_irqsave(&iommu->lock, flags);
  76. ret = __iommu_queue_command(iommu, cmd);
  77. spin_unlock_irqrestore(&iommu->lock, flags);
  78. return ret;
  79. }
  80. /*
  81. * This function is called whenever we need to ensure that the IOMMU has
  82. * completed execution of all commands we sent. It sends a
  83. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  84. * us about that by writing a value to a physical address we pass with
  85. * the command.
  86. */
  87. static int iommu_completion_wait(struct amd_iommu *iommu)
  88. {
  89. int ret, ready = 0;
  90. unsigned status = 0;
  91. struct iommu_cmd cmd;
  92. unsigned long i = 0;
  93. memset(&cmd, 0, sizeof(cmd));
  94. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  95. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  96. iommu->need_sync = 0;
  97. ret = iommu_queue_command(iommu, &cmd);
  98. if (ret)
  99. return ret;
  100. while (!ready && (i < EXIT_LOOP_COUNT)) {
  101. ++i;
  102. /* wait for the bit to become one */
  103. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  104. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  105. }
  106. /* set bit back to zero */
  107. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  108. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  109. if (unlikely((i == EXIT_LOOP_COUNT) && printk_ratelimit()))
  110. printk(KERN_WARNING "AMD IOMMU: Completion wait loop failed\n");
  111. return 0;
  112. }
  113. /*
  114. * Command send function for invalidating a device table entry
  115. */
  116. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  117. {
  118. struct iommu_cmd cmd;
  119. BUG_ON(iommu == NULL);
  120. memset(&cmd, 0, sizeof(cmd));
  121. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  122. cmd.data[0] = devid;
  123. iommu->need_sync = 1;
  124. return iommu_queue_command(iommu, &cmd);
  125. }
  126. /*
  127. * Generic command send function for invalidaing TLB entries
  128. */
  129. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  130. u64 address, u16 domid, int pde, int s)
  131. {
  132. struct iommu_cmd cmd;
  133. memset(&cmd, 0, sizeof(cmd));
  134. address &= PAGE_MASK;
  135. CMD_SET_TYPE(&cmd, CMD_INV_IOMMU_PAGES);
  136. cmd.data[1] |= domid;
  137. cmd.data[2] = lower_32_bits(address);
  138. cmd.data[3] = upper_32_bits(address);
  139. if (s) /* size bit - we flush more than one 4kb page */
  140. cmd.data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  141. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  142. cmd.data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  143. iommu->need_sync = 1;
  144. return iommu_queue_command(iommu, &cmd);
  145. }
  146. /*
  147. * TLB invalidation function which is called from the mapping functions.
  148. * It invalidates a single PTE if the range to flush is within a single
  149. * page. Otherwise it flushes the whole TLB of the IOMMU.
  150. */
  151. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  152. u64 address, size_t size)
  153. {
  154. int s = 0;
  155. unsigned pages = iommu_num_pages(address, size);
  156. address &= PAGE_MASK;
  157. if (pages > 1) {
  158. /*
  159. * If we have to flush more than one page, flush all
  160. * TLB entries for this domain
  161. */
  162. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  163. s = 1;
  164. }
  165. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  166. return 0;
  167. }
  168. /****************************************************************************
  169. *
  170. * The functions below are used the create the page table mappings for
  171. * unity mapped regions.
  172. *
  173. ****************************************************************************/
  174. /*
  175. * Generic mapping functions. It maps a physical address into a DMA
  176. * address space. It allocates the page table pages if necessary.
  177. * In the future it can be extended to a generic mapping function
  178. * supporting all features of AMD IOMMU page tables like level skipping
  179. * and full 64 bit address spaces.
  180. */
  181. static int iommu_map(struct protection_domain *dom,
  182. unsigned long bus_addr,
  183. unsigned long phys_addr,
  184. int prot)
  185. {
  186. u64 __pte, *pte, *page;
  187. bus_addr = PAGE_ALIGN(bus_addr);
  188. phys_addr = PAGE_ALIGN(bus_addr);
  189. /* only support 512GB address spaces for now */
  190. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  191. return -EINVAL;
  192. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  193. if (!IOMMU_PTE_PRESENT(*pte)) {
  194. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  195. if (!page)
  196. return -ENOMEM;
  197. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  198. }
  199. pte = IOMMU_PTE_PAGE(*pte);
  200. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  201. if (!IOMMU_PTE_PRESENT(*pte)) {
  202. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  203. if (!page)
  204. return -ENOMEM;
  205. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  206. }
  207. pte = IOMMU_PTE_PAGE(*pte);
  208. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  209. if (IOMMU_PTE_PRESENT(*pte))
  210. return -EBUSY;
  211. __pte = phys_addr | IOMMU_PTE_P;
  212. if (prot & IOMMU_PROT_IR)
  213. __pte |= IOMMU_PTE_IR;
  214. if (prot & IOMMU_PROT_IW)
  215. __pte |= IOMMU_PTE_IW;
  216. *pte = __pte;
  217. return 0;
  218. }
  219. /*
  220. * This function checks if a specific unity mapping entry is needed for
  221. * this specific IOMMU.
  222. */
  223. static int iommu_for_unity_map(struct amd_iommu *iommu,
  224. struct unity_map_entry *entry)
  225. {
  226. u16 bdf, i;
  227. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  228. bdf = amd_iommu_alias_table[i];
  229. if (amd_iommu_rlookup_table[bdf] == iommu)
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Init the unity mappings for a specific IOMMU in the system
  236. *
  237. * Basically iterates over all unity mapping entries and applies them to
  238. * the default domain DMA of that IOMMU if necessary.
  239. */
  240. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  241. {
  242. struct unity_map_entry *entry;
  243. int ret;
  244. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  245. if (!iommu_for_unity_map(iommu, entry))
  246. continue;
  247. ret = dma_ops_unity_map(iommu->default_dom, entry);
  248. if (ret)
  249. return ret;
  250. }
  251. return 0;
  252. }
  253. /*
  254. * This function actually applies the mapping to the page table of the
  255. * dma_ops domain.
  256. */
  257. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  258. struct unity_map_entry *e)
  259. {
  260. u64 addr;
  261. int ret;
  262. for (addr = e->address_start; addr < e->address_end;
  263. addr += PAGE_SIZE) {
  264. ret = iommu_map(&dma_dom->domain, addr, addr, e->prot);
  265. if (ret)
  266. return ret;
  267. /*
  268. * if unity mapping is in aperture range mark the page
  269. * as allocated in the aperture
  270. */
  271. if (addr < dma_dom->aperture_size)
  272. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  273. }
  274. return 0;
  275. }
  276. /*
  277. * Inits the unity mappings required for a specific device
  278. */
  279. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  280. u16 devid)
  281. {
  282. struct unity_map_entry *e;
  283. int ret;
  284. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  285. if (!(devid >= e->devid_start && devid <= e->devid_end))
  286. continue;
  287. ret = dma_ops_unity_map(dma_dom, e);
  288. if (ret)
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. /****************************************************************************
  294. *
  295. * The next functions belong to the address allocator for the dma_ops
  296. * interface functions. They work like the allocators in the other IOMMU
  297. * drivers. Its basically a bitmap which marks the allocated pages in
  298. * the aperture. Maybe it could be enhanced in the future to a more
  299. * efficient allocator.
  300. *
  301. ****************************************************************************/
  302. static unsigned long dma_mask_to_pages(unsigned long mask)
  303. {
  304. return (mask >> PAGE_SHIFT) +
  305. (PAGE_ALIGN(mask & ~PAGE_MASK) >> PAGE_SHIFT);
  306. }
  307. /*
  308. * The address allocator core function.
  309. *
  310. * called with domain->lock held
  311. */
  312. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  313. struct dma_ops_domain *dom,
  314. unsigned int pages)
  315. {
  316. unsigned long limit = dma_mask_to_pages(*dev->dma_mask);
  317. unsigned long address;
  318. unsigned long size = dom->aperture_size >> PAGE_SHIFT;
  319. unsigned long boundary_size;
  320. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  321. PAGE_SIZE) >> PAGE_SHIFT;
  322. limit = limit < size ? limit : size;
  323. if (dom->next_bit >= limit)
  324. dom->next_bit = 0;
  325. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  326. 0 , boundary_size, 0);
  327. if (address == -1)
  328. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  329. 0, boundary_size, 0);
  330. if (likely(address != -1)) {
  331. dom->next_bit = address + pages;
  332. address <<= PAGE_SHIFT;
  333. } else
  334. address = bad_dma_address;
  335. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  336. return address;
  337. }
  338. /*
  339. * The address free function.
  340. *
  341. * called with domain->lock held
  342. */
  343. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  344. unsigned long address,
  345. unsigned int pages)
  346. {
  347. address >>= PAGE_SHIFT;
  348. iommu_area_free(dom->bitmap, address, pages);
  349. }
  350. /****************************************************************************
  351. *
  352. * The next functions belong to the domain allocation. A domain is
  353. * allocated for every IOMMU as the default domain. If device isolation
  354. * is enabled, every device get its own domain. The most important thing
  355. * about domains is the page table mapping the DMA address space they
  356. * contain.
  357. *
  358. ****************************************************************************/
  359. static u16 domain_id_alloc(void)
  360. {
  361. unsigned long flags;
  362. int id;
  363. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  364. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  365. BUG_ON(id == 0);
  366. if (id > 0 && id < MAX_DOMAIN_ID)
  367. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  368. else
  369. id = 0;
  370. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  371. return id;
  372. }
  373. /*
  374. * Used to reserve address ranges in the aperture (e.g. for exclusion
  375. * ranges.
  376. */
  377. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  378. unsigned long start_page,
  379. unsigned int pages)
  380. {
  381. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  382. if (start_page + pages > last_page)
  383. pages = last_page - start_page;
  384. set_bit_string(dom->bitmap, start_page, pages);
  385. }
  386. static void dma_ops_free_pagetable(struct dma_ops_domain *dma_dom)
  387. {
  388. int i, j;
  389. u64 *p1, *p2, *p3;
  390. p1 = dma_dom->domain.pt_root;
  391. if (!p1)
  392. return;
  393. for (i = 0; i < 512; ++i) {
  394. if (!IOMMU_PTE_PRESENT(p1[i]))
  395. continue;
  396. p2 = IOMMU_PTE_PAGE(p1[i]);
  397. for (j = 0; j < 512; ++i) {
  398. if (!IOMMU_PTE_PRESENT(p2[j]))
  399. continue;
  400. p3 = IOMMU_PTE_PAGE(p2[j]);
  401. free_page((unsigned long)p3);
  402. }
  403. free_page((unsigned long)p2);
  404. }
  405. free_page((unsigned long)p1);
  406. }
  407. /*
  408. * Free a domain, only used if something went wrong in the
  409. * allocation path and we need to free an already allocated page table
  410. */
  411. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  412. {
  413. if (!dom)
  414. return;
  415. dma_ops_free_pagetable(dom);
  416. kfree(dom->pte_pages);
  417. kfree(dom->bitmap);
  418. kfree(dom);
  419. }
  420. /*
  421. * Allocates a new protection domain usable for the dma_ops functions.
  422. * It also intializes the page table and the address allocator data
  423. * structures required for the dma_ops interface
  424. */
  425. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  426. unsigned order)
  427. {
  428. struct dma_ops_domain *dma_dom;
  429. unsigned i, num_pte_pages;
  430. u64 *l2_pde;
  431. u64 address;
  432. /*
  433. * Currently the DMA aperture must be between 32 MB and 1GB in size
  434. */
  435. if ((order < 25) || (order > 30))
  436. return NULL;
  437. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  438. if (!dma_dom)
  439. return NULL;
  440. spin_lock_init(&dma_dom->domain.lock);
  441. dma_dom->domain.id = domain_id_alloc();
  442. if (dma_dom->domain.id == 0)
  443. goto free_dma_dom;
  444. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  445. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  446. dma_dom->domain.priv = dma_dom;
  447. if (!dma_dom->domain.pt_root)
  448. goto free_dma_dom;
  449. dma_dom->aperture_size = (1ULL << order);
  450. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  451. GFP_KERNEL);
  452. if (!dma_dom->bitmap)
  453. goto free_dma_dom;
  454. /*
  455. * mark the first page as allocated so we never return 0 as
  456. * a valid dma-address. So we can use 0 as error value
  457. */
  458. dma_dom->bitmap[0] = 1;
  459. dma_dom->next_bit = 0;
  460. /* Intialize the exclusion range if necessary */
  461. if (iommu->exclusion_start &&
  462. iommu->exclusion_start < dma_dom->aperture_size) {
  463. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  464. int pages = iommu_num_pages(iommu->exclusion_start,
  465. iommu->exclusion_length);
  466. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  467. }
  468. /*
  469. * At the last step, build the page tables so we don't need to
  470. * allocate page table pages in the dma_ops mapping/unmapping
  471. * path.
  472. */
  473. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  474. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  475. GFP_KERNEL);
  476. if (!dma_dom->pte_pages)
  477. goto free_dma_dom;
  478. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  479. if (l2_pde == NULL)
  480. goto free_dma_dom;
  481. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  482. for (i = 0; i < num_pte_pages; ++i) {
  483. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  484. if (!dma_dom->pte_pages[i])
  485. goto free_dma_dom;
  486. address = virt_to_phys(dma_dom->pte_pages[i]);
  487. l2_pde[i] = IOMMU_L1_PDE(address);
  488. }
  489. return dma_dom;
  490. free_dma_dom:
  491. dma_ops_domain_free(dma_dom);
  492. return NULL;
  493. }
  494. /*
  495. * Find out the protection domain structure for a given PCI device. This
  496. * will give us the pointer to the page table root for example.
  497. */
  498. static struct protection_domain *domain_for_device(u16 devid)
  499. {
  500. struct protection_domain *dom;
  501. unsigned long flags;
  502. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  503. dom = amd_iommu_pd_table[devid];
  504. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  505. return dom;
  506. }
  507. /*
  508. * If a device is not yet associated with a domain, this function does
  509. * assigns it visible for the hardware
  510. */
  511. static void set_device_domain(struct amd_iommu *iommu,
  512. struct protection_domain *domain,
  513. u16 devid)
  514. {
  515. unsigned long flags;
  516. u64 pte_root = virt_to_phys(domain->pt_root);
  517. pte_root |= (domain->mode & 0x07) << 9;
  518. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | 2;
  519. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  520. amd_iommu_dev_table[devid].data[0] = pte_root;
  521. amd_iommu_dev_table[devid].data[1] = pte_root >> 32;
  522. amd_iommu_dev_table[devid].data[2] = domain->id;
  523. amd_iommu_pd_table[devid] = domain;
  524. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  525. iommu_queue_inv_dev_entry(iommu, devid);
  526. iommu->need_sync = 1;
  527. }
  528. /*****************************************************************************
  529. *
  530. * The next functions belong to the dma_ops mapping/unmapping code.
  531. *
  532. *****************************************************************************/
  533. /*
  534. * In the dma_ops path we only have the struct device. This function
  535. * finds the corresponding IOMMU, the protection domain and the
  536. * requestor id for a given device.
  537. * If the device is not yet associated with a domain this is also done
  538. * in this function.
  539. */
  540. static int get_device_resources(struct device *dev,
  541. struct amd_iommu **iommu,
  542. struct protection_domain **domain,
  543. u16 *bdf)
  544. {
  545. struct dma_ops_domain *dma_dom;
  546. struct pci_dev *pcidev;
  547. u16 _bdf;
  548. BUG_ON(!dev || dev->bus != &pci_bus_type || !dev->dma_mask);
  549. pcidev = to_pci_dev(dev);
  550. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  551. /* device not translated by any IOMMU in the system? */
  552. if (_bdf > amd_iommu_last_bdf) {
  553. *iommu = NULL;
  554. *domain = NULL;
  555. *bdf = 0xffff;
  556. return 0;
  557. }
  558. *bdf = amd_iommu_alias_table[_bdf];
  559. *iommu = amd_iommu_rlookup_table[*bdf];
  560. if (*iommu == NULL)
  561. return 0;
  562. dma_dom = (*iommu)->default_dom;
  563. *domain = domain_for_device(*bdf);
  564. if (*domain == NULL) {
  565. *domain = &dma_dom->domain;
  566. set_device_domain(*iommu, *domain, *bdf);
  567. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  568. "device ", (*domain)->id);
  569. print_devid(_bdf, 1);
  570. }
  571. return 1;
  572. }
  573. /*
  574. * This is the generic map function. It maps one 4kb page at paddr to
  575. * the given address in the DMA address space for the domain.
  576. */
  577. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  578. struct dma_ops_domain *dom,
  579. unsigned long address,
  580. phys_addr_t paddr,
  581. int direction)
  582. {
  583. u64 *pte, __pte;
  584. WARN_ON(address > dom->aperture_size);
  585. paddr &= PAGE_MASK;
  586. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  587. pte += IOMMU_PTE_L0_INDEX(address);
  588. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  589. if (direction == DMA_TO_DEVICE)
  590. __pte |= IOMMU_PTE_IR;
  591. else if (direction == DMA_FROM_DEVICE)
  592. __pte |= IOMMU_PTE_IW;
  593. else if (direction == DMA_BIDIRECTIONAL)
  594. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  595. WARN_ON(*pte);
  596. *pte = __pte;
  597. return (dma_addr_t)address;
  598. }
  599. /*
  600. * The generic unmapping function for on page in the DMA address space.
  601. */
  602. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  603. struct dma_ops_domain *dom,
  604. unsigned long address)
  605. {
  606. u64 *pte;
  607. if (address >= dom->aperture_size)
  608. return;
  609. WARN_ON(address & 0xfffULL || address > dom->aperture_size);
  610. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  611. pte += IOMMU_PTE_L0_INDEX(address);
  612. WARN_ON(!*pte);
  613. *pte = 0ULL;
  614. }
  615. /*
  616. * This function contains common code for mapping of a physically
  617. * contiguous memory region into DMA address space. It is uses by all
  618. * mapping functions provided by this IOMMU driver.
  619. * Must be called with the domain lock held.
  620. */
  621. static dma_addr_t __map_single(struct device *dev,
  622. struct amd_iommu *iommu,
  623. struct dma_ops_domain *dma_dom,
  624. phys_addr_t paddr,
  625. size_t size,
  626. int dir)
  627. {
  628. dma_addr_t offset = paddr & ~PAGE_MASK;
  629. dma_addr_t address, start;
  630. unsigned int pages;
  631. int i;
  632. pages = iommu_num_pages(paddr, size);
  633. paddr &= PAGE_MASK;
  634. address = dma_ops_alloc_addresses(dev, dma_dom, pages);
  635. if (unlikely(address == bad_dma_address))
  636. goto out;
  637. start = address;
  638. for (i = 0; i < pages; ++i) {
  639. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  640. paddr += PAGE_SIZE;
  641. start += PAGE_SIZE;
  642. }
  643. address += offset;
  644. out:
  645. return address;
  646. }
  647. /*
  648. * Does the reverse of the __map_single function. Must be called with
  649. * the domain lock held too
  650. */
  651. static void __unmap_single(struct amd_iommu *iommu,
  652. struct dma_ops_domain *dma_dom,
  653. dma_addr_t dma_addr,
  654. size_t size,
  655. int dir)
  656. {
  657. dma_addr_t i, start;
  658. unsigned int pages;
  659. if ((dma_addr == 0) || (dma_addr + size > dma_dom->aperture_size))
  660. return;
  661. pages = iommu_num_pages(dma_addr, size);
  662. dma_addr &= PAGE_MASK;
  663. start = dma_addr;
  664. for (i = 0; i < pages; ++i) {
  665. dma_ops_domain_unmap(iommu, dma_dom, start);
  666. start += PAGE_SIZE;
  667. }
  668. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  669. }
  670. /*
  671. * The exported map_single function for dma_ops.
  672. */
  673. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  674. size_t size, int dir)
  675. {
  676. unsigned long flags;
  677. struct amd_iommu *iommu;
  678. struct protection_domain *domain;
  679. u16 devid;
  680. dma_addr_t addr;
  681. get_device_resources(dev, &iommu, &domain, &devid);
  682. if (iommu == NULL || domain == NULL)
  683. /* device not handled by any AMD IOMMU */
  684. return (dma_addr_t)paddr;
  685. spin_lock_irqsave(&domain->lock, flags);
  686. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir);
  687. if (addr == bad_dma_address)
  688. goto out;
  689. if (iommu_has_npcache(iommu))
  690. iommu_flush_pages(iommu, domain->id, addr, size);
  691. if (iommu->need_sync)
  692. iommu_completion_wait(iommu);
  693. out:
  694. spin_unlock_irqrestore(&domain->lock, flags);
  695. return addr;
  696. }
  697. /*
  698. * The exported unmap_single function for dma_ops.
  699. */
  700. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  701. size_t size, int dir)
  702. {
  703. unsigned long flags;
  704. struct amd_iommu *iommu;
  705. struct protection_domain *domain;
  706. u16 devid;
  707. if (!get_device_resources(dev, &iommu, &domain, &devid))
  708. /* device not handled by any AMD IOMMU */
  709. return;
  710. spin_lock_irqsave(&domain->lock, flags);
  711. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  712. iommu_flush_pages(iommu, domain->id, dma_addr, size);
  713. if (iommu->need_sync)
  714. iommu_completion_wait(iommu);
  715. spin_unlock_irqrestore(&domain->lock, flags);
  716. }
  717. /*
  718. * This is a special map_sg function which is used if we should map a
  719. * device which is not handled by an AMD IOMMU in the system.
  720. */
  721. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  722. int nelems, int dir)
  723. {
  724. struct scatterlist *s;
  725. int i;
  726. for_each_sg(sglist, s, nelems, i) {
  727. s->dma_address = (dma_addr_t)sg_phys(s);
  728. s->dma_length = s->length;
  729. }
  730. return nelems;
  731. }
  732. /*
  733. * The exported map_sg function for dma_ops (handles scatter-gather
  734. * lists).
  735. */
  736. static int map_sg(struct device *dev, struct scatterlist *sglist,
  737. int nelems, int dir)
  738. {
  739. unsigned long flags;
  740. struct amd_iommu *iommu;
  741. struct protection_domain *domain;
  742. u16 devid;
  743. int i;
  744. struct scatterlist *s;
  745. phys_addr_t paddr;
  746. int mapped_elems = 0;
  747. get_device_resources(dev, &iommu, &domain, &devid);
  748. if (!iommu || !domain)
  749. return map_sg_no_iommu(dev, sglist, nelems, dir);
  750. spin_lock_irqsave(&domain->lock, flags);
  751. for_each_sg(sglist, s, nelems, i) {
  752. paddr = sg_phys(s);
  753. s->dma_address = __map_single(dev, iommu, domain->priv,
  754. paddr, s->length, dir);
  755. if (s->dma_address) {
  756. s->dma_length = s->length;
  757. mapped_elems++;
  758. } else
  759. goto unmap;
  760. if (iommu_has_npcache(iommu))
  761. iommu_flush_pages(iommu, domain->id, s->dma_address,
  762. s->dma_length);
  763. }
  764. if (iommu->need_sync)
  765. iommu_completion_wait(iommu);
  766. out:
  767. spin_unlock_irqrestore(&domain->lock, flags);
  768. return mapped_elems;
  769. unmap:
  770. for_each_sg(sglist, s, mapped_elems, i) {
  771. if (s->dma_address)
  772. __unmap_single(iommu, domain->priv, s->dma_address,
  773. s->dma_length, dir);
  774. s->dma_address = s->dma_length = 0;
  775. }
  776. mapped_elems = 0;
  777. goto out;
  778. }
  779. /*
  780. * The exported map_sg function for dma_ops (handles scatter-gather
  781. * lists).
  782. */
  783. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  784. int nelems, int dir)
  785. {
  786. unsigned long flags;
  787. struct amd_iommu *iommu;
  788. struct protection_domain *domain;
  789. struct scatterlist *s;
  790. u16 devid;
  791. int i;
  792. if (!get_device_resources(dev, &iommu, &domain, &devid))
  793. return;
  794. spin_lock_irqsave(&domain->lock, flags);
  795. for_each_sg(sglist, s, nelems, i) {
  796. __unmap_single(iommu, domain->priv, s->dma_address,
  797. s->dma_length, dir);
  798. iommu_flush_pages(iommu, domain->id, s->dma_address,
  799. s->dma_length);
  800. s->dma_address = s->dma_length = 0;
  801. }
  802. if (iommu->need_sync)
  803. iommu_completion_wait(iommu);
  804. spin_unlock_irqrestore(&domain->lock, flags);
  805. }
  806. /*
  807. * The exported alloc_coherent function for dma_ops.
  808. */
  809. static void *alloc_coherent(struct device *dev, size_t size,
  810. dma_addr_t *dma_addr, gfp_t flag)
  811. {
  812. unsigned long flags;
  813. void *virt_addr;
  814. struct amd_iommu *iommu;
  815. struct protection_domain *domain;
  816. u16 devid;
  817. phys_addr_t paddr;
  818. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  819. if (!virt_addr)
  820. return 0;
  821. memset(virt_addr, 0, size);
  822. paddr = virt_to_phys(virt_addr);
  823. get_device_resources(dev, &iommu, &domain, &devid);
  824. if (!iommu || !domain) {
  825. *dma_addr = (dma_addr_t)paddr;
  826. return virt_addr;
  827. }
  828. spin_lock_irqsave(&domain->lock, flags);
  829. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  830. size, DMA_BIDIRECTIONAL);
  831. if (*dma_addr == bad_dma_address) {
  832. free_pages((unsigned long)virt_addr, get_order(size));
  833. virt_addr = NULL;
  834. goto out;
  835. }
  836. if (iommu_has_npcache(iommu))
  837. iommu_flush_pages(iommu, domain->id, *dma_addr, size);
  838. if (iommu->need_sync)
  839. iommu_completion_wait(iommu);
  840. out:
  841. spin_unlock_irqrestore(&domain->lock, flags);
  842. return virt_addr;
  843. }
  844. /*
  845. * The exported free_coherent function for dma_ops.
  846. */
  847. static void free_coherent(struct device *dev, size_t size,
  848. void *virt_addr, dma_addr_t dma_addr)
  849. {
  850. unsigned long flags;
  851. struct amd_iommu *iommu;
  852. struct protection_domain *domain;
  853. u16 devid;
  854. get_device_resources(dev, &iommu, &domain, &devid);
  855. if (!iommu || !domain)
  856. goto free_mem;
  857. spin_lock_irqsave(&domain->lock, flags);
  858. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  859. iommu_flush_pages(iommu, domain->id, dma_addr, size);
  860. if (iommu->need_sync)
  861. iommu_completion_wait(iommu);
  862. spin_unlock_irqrestore(&domain->lock, flags);
  863. free_mem:
  864. free_pages((unsigned long)virt_addr, get_order(size));
  865. }
  866. /*
  867. * The function for pre-allocating protection domains.
  868. *
  869. * If the driver core informs the DMA layer if a driver grabs a device
  870. * we don't need to preallocate the protection domains anymore.
  871. * For now we have to.
  872. */
  873. void prealloc_protection_domains(void)
  874. {
  875. struct pci_dev *dev = NULL;
  876. struct dma_ops_domain *dma_dom;
  877. struct amd_iommu *iommu;
  878. int order = amd_iommu_aperture_order;
  879. u16 devid;
  880. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  881. devid = (dev->bus->number << 8) | dev->devfn;
  882. if (devid > amd_iommu_last_bdf)
  883. continue;
  884. devid = amd_iommu_alias_table[devid];
  885. if (domain_for_device(devid))
  886. continue;
  887. iommu = amd_iommu_rlookup_table[devid];
  888. if (!iommu)
  889. continue;
  890. dma_dom = dma_ops_domain_alloc(iommu, order);
  891. if (!dma_dom)
  892. continue;
  893. init_unity_mappings_for_device(dma_dom, devid);
  894. set_device_domain(iommu, &dma_dom->domain, devid);
  895. printk(KERN_INFO "AMD IOMMU: Allocated domain %d for device ",
  896. dma_dom->domain.id);
  897. print_devid(devid, 1);
  898. }
  899. }
  900. static struct dma_mapping_ops amd_iommu_dma_ops = {
  901. .alloc_coherent = alloc_coherent,
  902. .free_coherent = free_coherent,
  903. .map_single = map_single,
  904. .unmap_single = unmap_single,
  905. .map_sg = map_sg,
  906. .unmap_sg = unmap_sg,
  907. };
  908. /*
  909. * The function which clues the AMD IOMMU driver into dma_ops.
  910. */
  911. int __init amd_iommu_init_dma_ops(void)
  912. {
  913. struct amd_iommu *iommu;
  914. int order = amd_iommu_aperture_order;
  915. int ret;
  916. /*
  917. * first allocate a default protection domain for every IOMMU we
  918. * found in the system. Devices not assigned to any other
  919. * protection domain will be assigned to the default one.
  920. */
  921. list_for_each_entry(iommu, &amd_iommu_list, list) {
  922. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  923. if (iommu->default_dom == NULL)
  924. return -ENOMEM;
  925. ret = iommu_init_unity_mappings(iommu);
  926. if (ret)
  927. goto free_domains;
  928. }
  929. /*
  930. * If device isolation is enabled, pre-allocate the protection
  931. * domains for each device.
  932. */
  933. if (amd_iommu_isolate)
  934. prealloc_protection_domains();
  935. iommu_detected = 1;
  936. force_iommu = 1;
  937. bad_dma_address = 0;
  938. #ifdef CONFIG_GART_IOMMU
  939. gart_iommu_aperture_disabled = 1;
  940. gart_iommu_aperture = 0;
  941. #endif
  942. /* Make the driver finally visible to the drivers */
  943. dma_ops = &amd_iommu_dma_ops;
  944. return 0;
  945. free_domains:
  946. list_for_each_entry(iommu, &amd_iommu_list, list) {
  947. if (iommu->default_dom)
  948. dma_ops_domain_free(iommu->default_dom);
  949. }
  950. return ret;
  951. }