amd_iommu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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/gart.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 to_pages(addr, size) \
  30. (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
  31. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  32. struct command {
  33. u32 data[4];
  34. };
  35. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  36. struct unity_map_entry *e);
  37. static int iommu_has_npcache(struct amd_iommu *iommu)
  38. {
  39. return iommu->cap & IOMMU_CAP_NPCACHE;
  40. }
  41. static int __iommu_queue_command(struct amd_iommu *iommu, struct command *cmd)
  42. {
  43. u32 tail, head;
  44. u8 *target;
  45. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  46. target = (iommu->cmd_buf + tail);
  47. memcpy_toio(target, cmd, sizeof(*cmd));
  48. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  49. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  50. if (tail == head)
  51. return -ENOMEM;
  52. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  53. return 0;
  54. }
  55. static int iommu_queue_command(struct amd_iommu *iommu, struct command *cmd)
  56. {
  57. unsigned long flags;
  58. int ret;
  59. spin_lock_irqsave(&iommu->lock, flags);
  60. ret = __iommu_queue_command(iommu, cmd);
  61. spin_unlock_irqrestore(&iommu->lock, flags);
  62. return ret;
  63. }
  64. static int iommu_completion_wait(struct amd_iommu *iommu)
  65. {
  66. int ret;
  67. struct command cmd;
  68. volatile u64 ready = 0;
  69. unsigned long ready_phys = virt_to_phys(&ready);
  70. memset(&cmd, 0, sizeof(cmd));
  71. cmd.data[0] = LOW_U32(ready_phys) | CMD_COMPL_WAIT_STORE_MASK;
  72. cmd.data[1] = HIGH_U32(ready_phys);
  73. cmd.data[2] = 1; /* value written to 'ready' */
  74. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  75. iommu->need_sync = 0;
  76. ret = iommu_queue_command(iommu, &cmd);
  77. if (ret)
  78. return ret;
  79. while (!ready)
  80. cpu_relax();
  81. return 0;
  82. }
  83. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  84. {
  85. struct command cmd;
  86. BUG_ON(iommu == NULL);
  87. memset(&cmd, 0, sizeof(cmd));
  88. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  89. cmd.data[0] = devid;
  90. iommu->need_sync = 1;
  91. return iommu_queue_command(iommu, &cmd);
  92. }
  93. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  94. u64 address, u16 domid, int pde, int s)
  95. {
  96. struct command cmd;
  97. memset(&cmd, 0, sizeof(cmd));
  98. address &= PAGE_MASK;
  99. CMD_SET_TYPE(&cmd, CMD_INV_IOMMU_PAGES);
  100. cmd.data[1] |= domid;
  101. cmd.data[2] = LOW_U32(address);
  102. cmd.data[3] = HIGH_U32(address);
  103. if (s)
  104. cmd.data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  105. if (pde)
  106. cmd.data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  107. iommu->need_sync = 1;
  108. return iommu_queue_command(iommu, &cmd);
  109. }
  110. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  111. u64 address, size_t size)
  112. {
  113. int s = 0;
  114. unsigned pages = to_pages(address, size);
  115. address &= PAGE_MASK;
  116. if (pages > 1) {
  117. /*
  118. * If we have to flush more than one page, flush all
  119. * TLB entries for this domain
  120. */
  121. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  122. s = 1;
  123. }
  124. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  125. return 0;
  126. }
  127. static int iommu_map(struct protection_domain *dom,
  128. unsigned long bus_addr,
  129. unsigned long phys_addr,
  130. int prot)
  131. {
  132. u64 __pte, *pte, *page;
  133. bus_addr = PAGE_ALIGN(bus_addr);
  134. phys_addr = PAGE_ALIGN(bus_addr);
  135. /* only support 512GB address spaces for now */
  136. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  137. return -EINVAL;
  138. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  139. if (!IOMMU_PTE_PRESENT(*pte)) {
  140. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  141. if (!page)
  142. return -ENOMEM;
  143. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  144. }
  145. pte = IOMMU_PTE_PAGE(*pte);
  146. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  147. if (!IOMMU_PTE_PRESENT(*pte)) {
  148. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  149. if (!page)
  150. return -ENOMEM;
  151. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  152. }
  153. pte = IOMMU_PTE_PAGE(*pte);
  154. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  155. if (IOMMU_PTE_PRESENT(*pte))
  156. return -EBUSY;
  157. __pte = phys_addr | IOMMU_PTE_P;
  158. if (prot & IOMMU_PROT_IR)
  159. __pte |= IOMMU_PTE_IR;
  160. if (prot & IOMMU_PROT_IW)
  161. __pte |= IOMMU_PTE_IW;
  162. *pte = __pte;
  163. return 0;
  164. }
  165. static int iommu_for_unity_map(struct amd_iommu *iommu,
  166. struct unity_map_entry *entry)
  167. {
  168. u16 bdf, i;
  169. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  170. bdf = amd_iommu_alias_table[i];
  171. if (amd_iommu_rlookup_table[bdf] == iommu)
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  177. {
  178. struct unity_map_entry *entry;
  179. int ret;
  180. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  181. if (!iommu_for_unity_map(iommu, entry))
  182. continue;
  183. ret = dma_ops_unity_map(iommu->default_dom, entry);
  184. if (ret)
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  190. struct unity_map_entry *e)
  191. {
  192. u64 addr;
  193. int ret;
  194. for (addr = e->address_start; addr < e->address_end;
  195. addr += PAGE_SIZE) {
  196. ret = iommu_map(&dma_dom->domain, addr, addr, e->prot);
  197. if (ret)
  198. return ret;
  199. /*
  200. * if unity mapping is in aperture range mark the page
  201. * as allocated in the aperture
  202. */
  203. if (addr < dma_dom->aperture_size)
  204. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  205. }
  206. return 0;
  207. }
  208. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  209. u16 devid)
  210. {
  211. struct unity_map_entry *e;
  212. int ret;
  213. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  214. if (!(devid >= e->devid_start && devid <= e->devid_end))
  215. continue;
  216. ret = dma_ops_unity_map(dma_dom, e);
  217. if (ret)
  218. return ret;
  219. }
  220. return 0;
  221. }
  222. static unsigned long dma_mask_to_pages(unsigned long mask)
  223. {
  224. return (mask >> PAGE_SHIFT) +
  225. (PAGE_ALIGN(mask & ~PAGE_MASK) >> PAGE_SHIFT);
  226. }
  227. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  228. struct dma_ops_domain *dom,
  229. unsigned int pages)
  230. {
  231. unsigned long limit = dma_mask_to_pages(*dev->dma_mask);
  232. unsigned long address;
  233. unsigned long size = dom->aperture_size >> PAGE_SHIFT;
  234. unsigned long boundary_size;
  235. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  236. PAGE_SIZE) >> PAGE_SHIFT;
  237. limit = limit < size ? limit : size;
  238. if (dom->next_bit >= limit)
  239. dom->next_bit = 0;
  240. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  241. 0 , boundary_size, 0);
  242. if (address == -1)
  243. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  244. 0, boundary_size, 0);
  245. if (likely(address != -1)) {
  246. dom->next_bit = address + pages;
  247. address <<= PAGE_SHIFT;
  248. } else
  249. address = bad_dma_address;
  250. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  251. return address;
  252. }
  253. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  254. unsigned long address,
  255. unsigned int pages)
  256. {
  257. address >>= PAGE_SHIFT;
  258. iommu_area_free(dom->bitmap, address, pages);
  259. }
  260. static u16 domain_id_alloc(void)
  261. {
  262. unsigned long flags;
  263. int id;
  264. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  265. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  266. BUG_ON(id == 0);
  267. if (id > 0 && id < MAX_DOMAIN_ID)
  268. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  269. else
  270. id = 0;
  271. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  272. return id;
  273. }
  274. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  275. unsigned long start_page,
  276. unsigned int pages)
  277. {
  278. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  279. if (start_page + pages > last_page)
  280. pages = last_page - start_page;
  281. set_bit_string(dom->bitmap, start_page, pages);
  282. }
  283. static void dma_ops_free_pagetable(struct dma_ops_domain *dma_dom)
  284. {
  285. int i, j;
  286. u64 *p1, *p2, *p3;
  287. p1 = dma_dom->domain.pt_root;
  288. if (!p1)
  289. return;
  290. for (i = 0; i < 512; ++i) {
  291. if (!IOMMU_PTE_PRESENT(p1[i]))
  292. continue;
  293. p2 = IOMMU_PTE_PAGE(p1[i]);
  294. for (j = 0; j < 512; ++i) {
  295. if (!IOMMU_PTE_PRESENT(p2[j]))
  296. continue;
  297. p3 = IOMMU_PTE_PAGE(p2[j]);
  298. free_page((unsigned long)p3);
  299. }
  300. free_page((unsigned long)p2);
  301. }
  302. free_page((unsigned long)p1);
  303. }
  304. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  305. {
  306. if (!dom)
  307. return;
  308. dma_ops_free_pagetable(dom);
  309. kfree(dom->pte_pages);
  310. kfree(dom->bitmap);
  311. kfree(dom);
  312. }
  313. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  314. unsigned order)
  315. {
  316. struct dma_ops_domain *dma_dom;
  317. unsigned i, num_pte_pages;
  318. u64 *l2_pde;
  319. u64 address;
  320. /*
  321. * Currently the DMA aperture must be between 32 MB and 1GB in size
  322. */
  323. if ((order < 25) || (order > 30))
  324. return NULL;
  325. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  326. if (!dma_dom)
  327. return NULL;
  328. spin_lock_init(&dma_dom->domain.lock);
  329. dma_dom->domain.id = domain_id_alloc();
  330. if (dma_dom->domain.id == 0)
  331. goto free_dma_dom;
  332. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  333. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  334. dma_dom->domain.priv = dma_dom;
  335. if (!dma_dom->domain.pt_root)
  336. goto free_dma_dom;
  337. dma_dom->aperture_size = (1ULL << order);
  338. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  339. GFP_KERNEL);
  340. if (!dma_dom->bitmap)
  341. goto free_dma_dom;
  342. /*
  343. * mark the first page as allocated so we never return 0 as
  344. * a valid dma-address. So we can use 0 as error value
  345. */
  346. dma_dom->bitmap[0] = 1;
  347. dma_dom->next_bit = 0;
  348. if (iommu->exclusion_start &&
  349. iommu->exclusion_start < dma_dom->aperture_size) {
  350. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  351. int pages = to_pages(iommu->exclusion_start,
  352. iommu->exclusion_length);
  353. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  354. }
  355. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  356. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  357. GFP_KERNEL);
  358. if (!dma_dom->pte_pages)
  359. goto free_dma_dom;
  360. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  361. if (l2_pde == NULL)
  362. goto free_dma_dom;
  363. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  364. for (i = 0; i < num_pte_pages; ++i) {
  365. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  366. if (!dma_dom->pte_pages[i])
  367. goto free_dma_dom;
  368. address = virt_to_phys(dma_dom->pte_pages[i]);
  369. l2_pde[i] = IOMMU_L1_PDE(address);
  370. }
  371. return dma_dom;
  372. free_dma_dom:
  373. dma_ops_domain_free(dma_dom);
  374. return NULL;
  375. }
  376. static struct protection_domain *domain_for_device(u16 devid)
  377. {
  378. struct protection_domain *dom;
  379. unsigned long flags;
  380. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  381. dom = amd_iommu_pd_table[devid];
  382. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  383. return dom;
  384. }
  385. static void set_device_domain(struct amd_iommu *iommu,
  386. struct protection_domain *domain,
  387. u16 devid)
  388. {
  389. unsigned long flags;
  390. u64 pte_root = virt_to_phys(domain->pt_root);
  391. pte_root |= (domain->mode & 0x07) << 9;
  392. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | 2;
  393. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  394. amd_iommu_dev_table[devid].data[0] = pte_root;
  395. amd_iommu_dev_table[devid].data[1] = pte_root >> 32;
  396. amd_iommu_dev_table[devid].data[2] = domain->id;
  397. amd_iommu_pd_table[devid] = domain;
  398. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  399. iommu_queue_inv_dev_entry(iommu, devid);
  400. iommu->need_sync = 1;
  401. }
  402. static int get_device_resources(struct device *dev,
  403. struct amd_iommu **iommu,
  404. struct protection_domain **domain,
  405. u16 *bdf)
  406. {
  407. struct dma_ops_domain *dma_dom;
  408. struct pci_dev *pcidev;
  409. u16 _bdf;
  410. BUG_ON(!dev || dev->bus != &pci_bus_type || !dev->dma_mask);
  411. pcidev = to_pci_dev(dev);
  412. _bdf = (pcidev->bus->number << 8) | pcidev->devfn;
  413. if (_bdf >= amd_iommu_last_bdf) {
  414. *iommu = NULL;
  415. *domain = NULL;
  416. *bdf = 0xffff;
  417. return 0;
  418. }
  419. *bdf = amd_iommu_alias_table[_bdf];
  420. *iommu = amd_iommu_rlookup_table[*bdf];
  421. if (*iommu == NULL)
  422. return 0;
  423. dma_dom = (*iommu)->default_dom;
  424. *domain = domain_for_device(*bdf);
  425. if (*domain == NULL) {
  426. *domain = &dma_dom->domain;
  427. set_device_domain(*iommu, *domain, *bdf);
  428. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  429. "device ", (*domain)->id);
  430. print_devid(_bdf, 1);
  431. }
  432. return 1;
  433. }
  434. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  435. struct dma_ops_domain *dom,
  436. unsigned long address,
  437. phys_addr_t paddr,
  438. int direction)
  439. {
  440. u64 *pte, __pte;
  441. WARN_ON(address > dom->aperture_size);
  442. paddr &= PAGE_MASK;
  443. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  444. pte += IOMMU_PTE_L0_INDEX(address);
  445. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  446. if (direction == DMA_TO_DEVICE)
  447. __pte |= IOMMU_PTE_IR;
  448. else if (direction == DMA_FROM_DEVICE)
  449. __pte |= IOMMU_PTE_IW;
  450. else if (direction == DMA_BIDIRECTIONAL)
  451. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  452. WARN_ON(*pte);
  453. *pte = __pte;
  454. return (dma_addr_t)address;
  455. }
  456. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  457. struct dma_ops_domain *dom,
  458. unsigned long address)
  459. {
  460. u64 *pte;
  461. if (address >= dom->aperture_size)
  462. return;
  463. WARN_ON(address & 0xfffULL || address > dom->aperture_size);
  464. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  465. pte += IOMMU_PTE_L0_INDEX(address);
  466. WARN_ON(!*pte);
  467. *pte = 0ULL;
  468. }
  469. static dma_addr_t __map_single(struct device *dev,
  470. struct amd_iommu *iommu,
  471. struct dma_ops_domain *dma_dom,
  472. phys_addr_t paddr,
  473. size_t size,
  474. int dir)
  475. {
  476. dma_addr_t offset = paddr & ~PAGE_MASK;
  477. dma_addr_t address, start;
  478. unsigned int pages;
  479. int i;
  480. pages = to_pages(paddr, size);
  481. paddr &= PAGE_MASK;
  482. address = dma_ops_alloc_addresses(dev, dma_dom, pages);
  483. if (unlikely(address == bad_dma_address))
  484. goto out;
  485. start = address;
  486. for (i = 0; i < pages; ++i) {
  487. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  488. paddr += PAGE_SIZE;
  489. start += PAGE_SIZE;
  490. }
  491. address += offset;
  492. out:
  493. return address;
  494. }
  495. static void __unmap_single(struct amd_iommu *iommu,
  496. struct dma_ops_domain *dma_dom,
  497. dma_addr_t dma_addr,
  498. size_t size,
  499. int dir)
  500. {
  501. dma_addr_t i, start;
  502. unsigned int pages;
  503. if ((dma_addr == 0) || (dma_addr + size > dma_dom->aperture_size))
  504. return;
  505. pages = to_pages(dma_addr, size);
  506. dma_addr &= PAGE_MASK;
  507. start = dma_addr;
  508. for (i = 0; i < pages; ++i) {
  509. dma_ops_domain_unmap(iommu, dma_dom, start);
  510. start += PAGE_SIZE;
  511. }
  512. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  513. }
  514. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  515. size_t size, int dir)
  516. {
  517. unsigned long flags;
  518. struct amd_iommu *iommu;
  519. struct protection_domain *domain;
  520. u16 devid;
  521. dma_addr_t addr;
  522. get_device_resources(dev, &iommu, &domain, &devid);
  523. if (iommu == NULL || domain == NULL)
  524. return (dma_addr_t)paddr;
  525. spin_lock_irqsave(&domain->lock, flags);
  526. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir);
  527. if (addr == bad_dma_address)
  528. goto out;
  529. if (iommu_has_npcache(iommu))
  530. iommu_flush_pages(iommu, domain->id, addr, size);
  531. if (iommu->need_sync)
  532. iommu_completion_wait(iommu);
  533. out:
  534. spin_unlock_irqrestore(&domain->lock, flags);
  535. return addr;
  536. }
  537. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  538. size_t size, int dir)
  539. {
  540. unsigned long flags;
  541. struct amd_iommu *iommu;
  542. struct protection_domain *domain;
  543. u16 devid;
  544. if (!get_device_resources(dev, &iommu, &domain, &devid))
  545. return;
  546. spin_lock_irqsave(&domain->lock, flags);
  547. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  548. iommu_flush_pages(iommu, domain->id, dma_addr, size);
  549. if (iommu->need_sync)
  550. iommu_completion_wait(iommu);
  551. spin_unlock_irqrestore(&domain->lock, flags);
  552. }
  553. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  554. int nelems, int dir)
  555. {
  556. struct scatterlist *s;
  557. int i;
  558. for_each_sg(sglist, s, nelems, i) {
  559. s->dma_address = (dma_addr_t)sg_phys(s);
  560. s->dma_length = s->length;
  561. }
  562. return nelems;
  563. }
  564. static int map_sg(struct device *dev, struct scatterlist *sglist,
  565. int nelems, int dir)
  566. {
  567. unsigned long flags;
  568. struct amd_iommu *iommu;
  569. struct protection_domain *domain;
  570. u16 devid;
  571. int i;
  572. struct scatterlist *s;
  573. phys_addr_t paddr;
  574. int mapped_elems = 0;
  575. get_device_resources(dev, &iommu, &domain, &devid);
  576. if (!iommu || !domain)
  577. return map_sg_no_iommu(dev, sglist, nelems, dir);
  578. spin_lock_irqsave(&domain->lock, flags);
  579. for_each_sg(sglist, s, nelems, i) {
  580. paddr = sg_phys(s);
  581. s->dma_address = __map_single(dev, iommu, domain->priv,
  582. paddr, s->length, dir);
  583. if (s->dma_address) {
  584. s->dma_length = s->length;
  585. mapped_elems++;
  586. } else
  587. goto unmap;
  588. if (iommu_has_npcache(iommu))
  589. iommu_flush_pages(iommu, domain->id, s->dma_address,
  590. s->dma_length);
  591. }
  592. if (iommu->need_sync)
  593. iommu_completion_wait(iommu);
  594. out:
  595. spin_unlock_irqrestore(&domain->lock, flags);
  596. return mapped_elems;
  597. unmap:
  598. for_each_sg(sglist, s, mapped_elems, i) {
  599. if (s->dma_address)
  600. __unmap_single(iommu, domain->priv, s->dma_address,
  601. s->dma_length, dir);
  602. s->dma_address = s->dma_length = 0;
  603. }
  604. mapped_elems = 0;
  605. goto out;
  606. }
  607. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  608. int nelems, int dir)
  609. {
  610. unsigned long flags;
  611. struct amd_iommu *iommu;
  612. struct protection_domain *domain;
  613. struct scatterlist *s;
  614. u16 devid;
  615. int i;
  616. if (!get_device_resources(dev, &iommu, &domain, &devid))
  617. return;
  618. spin_lock_irqsave(&domain->lock, flags);
  619. for_each_sg(sglist, s, nelems, i) {
  620. __unmap_single(iommu, domain->priv, s->dma_address,
  621. s->dma_length, dir);
  622. iommu_flush_pages(iommu, domain->id, s->dma_address,
  623. s->dma_length);
  624. s->dma_address = s->dma_length = 0;
  625. }
  626. if (iommu->need_sync)
  627. iommu_completion_wait(iommu);
  628. spin_unlock_irqrestore(&domain->lock, flags);
  629. }
  630. static void *alloc_coherent(struct device *dev, size_t size,
  631. dma_addr_t *dma_addr, gfp_t flag)
  632. {
  633. unsigned long flags;
  634. void *virt_addr;
  635. struct amd_iommu *iommu;
  636. struct protection_domain *domain;
  637. u16 devid;
  638. phys_addr_t paddr;
  639. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  640. if (!virt_addr)
  641. return 0;
  642. memset(virt_addr, 0, size);
  643. paddr = virt_to_phys(virt_addr);
  644. get_device_resources(dev, &iommu, &domain, &devid);
  645. if (!iommu || !domain) {
  646. *dma_addr = (dma_addr_t)paddr;
  647. return virt_addr;
  648. }
  649. spin_lock_irqsave(&domain->lock, flags);
  650. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  651. size, DMA_BIDIRECTIONAL);
  652. if (*dma_addr == bad_dma_address) {
  653. free_pages((unsigned long)virt_addr, get_order(size));
  654. virt_addr = NULL;
  655. goto out;
  656. }
  657. if (iommu_has_npcache(iommu))
  658. iommu_flush_pages(iommu, domain->id, *dma_addr, size);
  659. if (iommu->need_sync)
  660. iommu_completion_wait(iommu);
  661. out:
  662. spin_unlock_irqrestore(&domain->lock, flags);
  663. return virt_addr;
  664. }
  665. static void free_coherent(struct device *dev, size_t size,
  666. void *virt_addr, dma_addr_t dma_addr)
  667. {
  668. unsigned long flags;
  669. struct amd_iommu *iommu;
  670. struct protection_domain *domain;
  671. u16 devid;
  672. get_device_resources(dev, &iommu, &domain, &devid);
  673. if (!iommu || !domain)
  674. goto free_mem;
  675. spin_lock_irqsave(&domain->lock, flags);
  676. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  677. iommu_flush_pages(iommu, domain->id, dma_addr, size);
  678. if (iommu->need_sync)
  679. iommu_completion_wait(iommu);
  680. spin_unlock_irqrestore(&domain->lock, flags);
  681. free_mem:
  682. free_pages((unsigned long)virt_addr, get_order(size));
  683. }
  684. /*
  685. * If the driver core informs the DMA layer if a driver grabs a device
  686. * we don't need to preallocate the protection domains anymore.
  687. * For now we have to.
  688. */
  689. void prealloc_protection_domains(void)
  690. {
  691. struct pci_dev *dev = NULL;
  692. struct dma_ops_domain *dma_dom;
  693. struct amd_iommu *iommu;
  694. int order = amd_iommu_aperture_order;
  695. u16 devid;
  696. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  697. devid = (dev->bus->number << 8) | dev->devfn;
  698. if (devid >= amd_iommu_last_bdf)
  699. continue;
  700. devid = amd_iommu_alias_table[devid];
  701. if (domain_for_device(devid))
  702. continue;
  703. iommu = amd_iommu_rlookup_table[devid];
  704. if (!iommu)
  705. continue;
  706. dma_dom = dma_ops_domain_alloc(iommu, order);
  707. if (!dma_dom)
  708. continue;
  709. init_unity_mappings_for_device(dma_dom, devid);
  710. set_device_domain(iommu, &dma_dom->domain, devid);
  711. printk(KERN_INFO "AMD IOMMU: Allocated domain %d for device ",
  712. dma_dom->domain.id);
  713. print_devid(devid, 1);
  714. }
  715. }
  716. static struct dma_mapping_ops amd_iommu_dma_ops = {
  717. .alloc_coherent = alloc_coherent,
  718. .free_coherent = free_coherent,
  719. .map_single = map_single,
  720. .unmap_single = unmap_single,
  721. .map_sg = map_sg,
  722. .unmap_sg = unmap_sg,
  723. };
  724. int __init amd_iommu_init_dma_ops(void)
  725. {
  726. struct amd_iommu *iommu;
  727. int order = amd_iommu_aperture_order;
  728. int ret;
  729. list_for_each_entry(iommu, &amd_iommu_list, list) {
  730. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  731. if (iommu->default_dom == NULL)
  732. return -ENOMEM;
  733. ret = iommu_init_unity_mappings(iommu);
  734. if (ret)
  735. goto free_domains;
  736. }
  737. if (amd_iommu_isolate)
  738. prealloc_protection_domains();
  739. iommu_detected = 1;
  740. force_iommu = 1;
  741. bad_dma_address = 0;
  742. #ifdef CONFIG_GART_IOMMU
  743. gart_iommu_aperture_disabled = 1;
  744. gart_iommu_aperture = 0;
  745. #endif
  746. dma_ops = &amd_iommu_dma_ops;
  747. return 0;
  748. free_domains:
  749. list_for_each_entry(iommu, &amd_iommu_list, list) {
  750. if (iommu->default_dom)
  751. dma_ops_domain_free(iommu->default_dom);
  752. }
  753. return ret;
  754. }