amd_iommu.c 29 KB

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