amd_iommu.c 29 KB

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