amd_iommu.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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. #ifdef CONFIG_IOMMU_API
  25. #include <linux/iommu.h>
  26. #endif
  27. #include <asm/proto.h>
  28. #include <asm/iommu.h>
  29. #include <asm/gart.h>
  30. #include <asm/amd_iommu_types.h>
  31. #include <asm/amd_iommu.h>
  32. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  33. #define EXIT_LOOP_COUNT 10000000
  34. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  35. /* A list of preallocated protection domains */
  36. static LIST_HEAD(iommu_pd_list);
  37. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  38. /*
  39. * general struct to manage commands send to an IOMMU
  40. */
  41. struct iommu_cmd {
  42. u32 data[4];
  43. };
  44. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  45. struct unity_map_entry *e);
  46. static struct dma_ops_domain *find_protection_domain(u16 devid);
  47. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  48. static int iommu_has_npcache(struct amd_iommu *iommu)
  49. {
  50. return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
  51. }
  52. /****************************************************************************
  53. *
  54. * Interrupt handling functions
  55. *
  56. ****************************************************************************/
  57. static void iommu_print_event(void *__evt)
  58. {
  59. u32 *event = __evt;
  60. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  61. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  62. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  63. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  64. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  65. printk(KERN_ERR "AMD IOMMU: Event logged [");
  66. switch (type) {
  67. case EVENT_TYPE_ILL_DEV:
  68. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  69. "address=0x%016llx flags=0x%04x]\n",
  70. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  71. address, flags);
  72. break;
  73. case EVENT_TYPE_IO_FAULT:
  74. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  75. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  76. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  77. domid, address, flags);
  78. break;
  79. case EVENT_TYPE_DEV_TAB_ERR:
  80. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  81. "address=0x%016llx flags=0x%04x]\n",
  82. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  83. address, flags);
  84. break;
  85. case EVENT_TYPE_PAGE_TAB_ERR:
  86. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  87. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  88. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  89. domid, address, flags);
  90. break;
  91. case EVENT_TYPE_ILL_CMD:
  92. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  93. break;
  94. case EVENT_TYPE_CMD_HARD_ERR:
  95. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  96. "flags=0x%04x]\n", address, flags);
  97. break;
  98. case EVENT_TYPE_IOTLB_INV_TO:
  99. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  100. "address=0x%016llx]\n",
  101. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  102. address);
  103. break;
  104. case EVENT_TYPE_INV_DEV_REQ:
  105. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  106. "address=0x%016llx flags=0x%04x]\n",
  107. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  108. address, flags);
  109. break;
  110. default:
  111. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  112. }
  113. }
  114. static void iommu_poll_events(struct amd_iommu *iommu)
  115. {
  116. u32 head, tail;
  117. unsigned long flags;
  118. spin_lock_irqsave(&iommu->lock, flags);
  119. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  120. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  121. while (head != tail) {
  122. iommu_print_event(iommu->evt_buf + head);
  123. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  124. }
  125. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  126. spin_unlock_irqrestore(&iommu->lock, flags);
  127. }
  128. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  129. {
  130. struct amd_iommu *iommu;
  131. list_for_each_entry(iommu, &amd_iommu_list, list)
  132. iommu_poll_events(iommu);
  133. return IRQ_HANDLED;
  134. }
  135. /****************************************************************************
  136. *
  137. * IOMMU command queuing functions
  138. *
  139. ****************************************************************************/
  140. /*
  141. * Writes the command to the IOMMUs command buffer and informs the
  142. * hardware about the new command. Must be called with iommu->lock held.
  143. */
  144. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  145. {
  146. u32 tail, head;
  147. u8 *target;
  148. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  149. target = iommu->cmd_buf + tail;
  150. memcpy_toio(target, cmd, sizeof(*cmd));
  151. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  152. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  153. if (tail == head)
  154. return -ENOMEM;
  155. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  156. return 0;
  157. }
  158. /*
  159. * General queuing function for commands. Takes iommu->lock and calls
  160. * __iommu_queue_command().
  161. */
  162. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  163. {
  164. unsigned long flags;
  165. int ret;
  166. spin_lock_irqsave(&iommu->lock, flags);
  167. ret = __iommu_queue_command(iommu, cmd);
  168. if (!ret)
  169. iommu->need_sync = 1;
  170. spin_unlock_irqrestore(&iommu->lock, flags);
  171. return ret;
  172. }
  173. /*
  174. * This function waits until an IOMMU has completed a completion
  175. * wait command
  176. */
  177. static void __iommu_wait_for_completion(struct amd_iommu *iommu)
  178. {
  179. int ready = 0;
  180. unsigned status = 0;
  181. unsigned long i = 0;
  182. while (!ready && (i < EXIT_LOOP_COUNT)) {
  183. ++i;
  184. /* wait for the bit to become one */
  185. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  186. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  187. }
  188. /* set bit back to zero */
  189. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  190. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  191. if (unlikely(i == EXIT_LOOP_COUNT))
  192. panic("AMD IOMMU: Completion wait loop failed\n");
  193. }
  194. /*
  195. * This function queues a completion wait command into the command
  196. * buffer of an IOMMU
  197. */
  198. static int __iommu_completion_wait(struct amd_iommu *iommu)
  199. {
  200. struct iommu_cmd cmd;
  201. memset(&cmd, 0, sizeof(cmd));
  202. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  203. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  204. return __iommu_queue_command(iommu, &cmd);
  205. }
  206. /*
  207. * This function is called whenever we need to ensure that the IOMMU has
  208. * completed execution of all commands we sent. It sends a
  209. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  210. * us about that by writing a value to a physical address we pass with
  211. * the command.
  212. */
  213. static int iommu_completion_wait(struct amd_iommu *iommu)
  214. {
  215. int ret = 0;
  216. unsigned long flags;
  217. spin_lock_irqsave(&iommu->lock, flags);
  218. if (!iommu->need_sync)
  219. goto out;
  220. ret = __iommu_completion_wait(iommu);
  221. iommu->need_sync = 0;
  222. if (ret)
  223. goto out;
  224. __iommu_wait_for_completion(iommu);
  225. out:
  226. spin_unlock_irqrestore(&iommu->lock, flags);
  227. return 0;
  228. }
  229. /*
  230. * Command send function for invalidating a device table entry
  231. */
  232. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  233. {
  234. struct iommu_cmd cmd;
  235. int ret;
  236. BUG_ON(iommu == NULL);
  237. memset(&cmd, 0, sizeof(cmd));
  238. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  239. cmd.data[0] = devid;
  240. ret = iommu_queue_command(iommu, &cmd);
  241. return ret;
  242. }
  243. static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  244. u16 domid, int pde, int s)
  245. {
  246. memset(cmd, 0, sizeof(*cmd));
  247. address &= PAGE_MASK;
  248. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  249. cmd->data[1] |= domid;
  250. cmd->data[2] = lower_32_bits(address);
  251. cmd->data[3] = upper_32_bits(address);
  252. if (s) /* size bit - we flush more than one 4kb page */
  253. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  254. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  255. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  256. }
  257. /*
  258. * Generic command send function for invalidaing TLB entries
  259. */
  260. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  261. u64 address, u16 domid, int pde, int s)
  262. {
  263. struct iommu_cmd cmd;
  264. int ret;
  265. __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
  266. ret = iommu_queue_command(iommu, &cmd);
  267. return ret;
  268. }
  269. /*
  270. * TLB invalidation function which is called from the mapping functions.
  271. * It invalidates a single PTE if the range to flush is within a single
  272. * page. Otherwise it flushes the whole TLB of the IOMMU.
  273. */
  274. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  275. u64 address, size_t size)
  276. {
  277. int s = 0;
  278. unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
  279. address &= PAGE_MASK;
  280. if (pages > 1) {
  281. /*
  282. * If we have to flush more than one page, flush all
  283. * TLB entries for this domain
  284. */
  285. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  286. s = 1;
  287. }
  288. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  289. return 0;
  290. }
  291. /* Flush the whole IO/TLB for a given protection domain */
  292. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  293. {
  294. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  295. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  296. }
  297. #ifdef CONFIG_IOMMU_API
  298. /*
  299. * This function is used to flush the IO/TLB for a given protection domain
  300. * on every IOMMU in the system
  301. */
  302. static void iommu_flush_domain(u16 domid)
  303. {
  304. unsigned long flags;
  305. struct amd_iommu *iommu;
  306. struct iommu_cmd cmd;
  307. __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  308. domid, 1, 1);
  309. list_for_each_entry(iommu, &amd_iommu_list, list) {
  310. spin_lock_irqsave(&iommu->lock, flags);
  311. __iommu_queue_command(iommu, &cmd);
  312. __iommu_completion_wait(iommu);
  313. __iommu_wait_for_completion(iommu);
  314. spin_unlock_irqrestore(&iommu->lock, flags);
  315. }
  316. }
  317. #endif
  318. /****************************************************************************
  319. *
  320. * The functions below are used the create the page table mappings for
  321. * unity mapped regions.
  322. *
  323. ****************************************************************************/
  324. /*
  325. * Generic mapping functions. It maps a physical address into a DMA
  326. * address space. It allocates the page table pages if necessary.
  327. * In the future it can be extended to a generic mapping function
  328. * supporting all features of AMD IOMMU page tables like level skipping
  329. * and full 64 bit address spaces.
  330. */
  331. static int iommu_map_page(struct protection_domain *dom,
  332. unsigned long bus_addr,
  333. unsigned long phys_addr,
  334. int prot)
  335. {
  336. u64 __pte, *pte, *page;
  337. bus_addr = PAGE_ALIGN(bus_addr);
  338. phys_addr = PAGE_ALIGN(phys_addr);
  339. /* only support 512GB address spaces for now */
  340. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  341. return -EINVAL;
  342. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  343. if (!IOMMU_PTE_PRESENT(*pte)) {
  344. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  345. if (!page)
  346. return -ENOMEM;
  347. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  348. }
  349. pte = IOMMU_PTE_PAGE(*pte);
  350. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  351. if (!IOMMU_PTE_PRESENT(*pte)) {
  352. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  353. if (!page)
  354. return -ENOMEM;
  355. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  356. }
  357. pte = IOMMU_PTE_PAGE(*pte);
  358. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  359. if (IOMMU_PTE_PRESENT(*pte))
  360. return -EBUSY;
  361. __pte = phys_addr | IOMMU_PTE_P;
  362. if (prot & IOMMU_PROT_IR)
  363. __pte |= IOMMU_PTE_IR;
  364. if (prot & IOMMU_PROT_IW)
  365. __pte |= IOMMU_PTE_IW;
  366. *pte = __pte;
  367. return 0;
  368. }
  369. #ifdef CONFIG_IOMMU_API
  370. static void iommu_unmap_page(struct protection_domain *dom,
  371. unsigned long bus_addr)
  372. {
  373. u64 *pte;
  374. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  375. if (!IOMMU_PTE_PRESENT(*pte))
  376. return;
  377. pte = IOMMU_PTE_PAGE(*pte);
  378. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  379. if (!IOMMU_PTE_PRESENT(*pte))
  380. return;
  381. pte = IOMMU_PTE_PAGE(*pte);
  382. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  383. *pte = 0;
  384. }
  385. #endif
  386. /*
  387. * This function checks if a specific unity mapping entry is needed for
  388. * this specific IOMMU.
  389. */
  390. static int iommu_for_unity_map(struct amd_iommu *iommu,
  391. struct unity_map_entry *entry)
  392. {
  393. u16 bdf, i;
  394. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  395. bdf = amd_iommu_alias_table[i];
  396. if (amd_iommu_rlookup_table[bdf] == iommu)
  397. return 1;
  398. }
  399. return 0;
  400. }
  401. /*
  402. * Init the unity mappings for a specific IOMMU in the system
  403. *
  404. * Basically iterates over all unity mapping entries and applies them to
  405. * the default domain DMA of that IOMMU if necessary.
  406. */
  407. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  408. {
  409. struct unity_map_entry *entry;
  410. int ret;
  411. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  412. if (!iommu_for_unity_map(iommu, entry))
  413. continue;
  414. ret = dma_ops_unity_map(iommu->default_dom, entry);
  415. if (ret)
  416. return ret;
  417. }
  418. return 0;
  419. }
  420. /*
  421. * This function actually applies the mapping to the page table of the
  422. * dma_ops domain.
  423. */
  424. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  425. struct unity_map_entry *e)
  426. {
  427. u64 addr;
  428. int ret;
  429. for (addr = e->address_start; addr < e->address_end;
  430. addr += PAGE_SIZE) {
  431. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot);
  432. if (ret)
  433. return ret;
  434. /*
  435. * if unity mapping is in aperture range mark the page
  436. * as allocated in the aperture
  437. */
  438. if (addr < dma_dom->aperture_size)
  439. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  440. }
  441. return 0;
  442. }
  443. /*
  444. * Inits the unity mappings required for a specific device
  445. */
  446. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  447. u16 devid)
  448. {
  449. struct unity_map_entry *e;
  450. int ret;
  451. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  452. if (!(devid >= e->devid_start && devid <= e->devid_end))
  453. continue;
  454. ret = dma_ops_unity_map(dma_dom, e);
  455. if (ret)
  456. return ret;
  457. }
  458. return 0;
  459. }
  460. /****************************************************************************
  461. *
  462. * The next functions belong to the address allocator for the dma_ops
  463. * interface functions. They work like the allocators in the other IOMMU
  464. * drivers. Its basically a bitmap which marks the allocated pages in
  465. * the aperture. Maybe it could be enhanced in the future to a more
  466. * efficient allocator.
  467. *
  468. ****************************************************************************/
  469. /*
  470. * The address allocator core function.
  471. *
  472. * called with domain->lock held
  473. */
  474. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  475. struct dma_ops_domain *dom,
  476. unsigned int pages,
  477. unsigned long align_mask,
  478. u64 dma_mask)
  479. {
  480. unsigned long limit;
  481. unsigned long address;
  482. unsigned long boundary_size;
  483. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  484. PAGE_SIZE) >> PAGE_SHIFT;
  485. limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
  486. dma_mask >> PAGE_SHIFT);
  487. if (dom->next_bit >= limit) {
  488. dom->next_bit = 0;
  489. dom->need_flush = true;
  490. }
  491. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  492. 0 , boundary_size, align_mask);
  493. if (address == -1) {
  494. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  495. 0, boundary_size, align_mask);
  496. dom->need_flush = true;
  497. }
  498. if (likely(address != -1)) {
  499. dom->next_bit = address + pages;
  500. address <<= PAGE_SHIFT;
  501. } else
  502. address = bad_dma_address;
  503. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  504. return address;
  505. }
  506. /*
  507. * The address free function.
  508. *
  509. * called with domain->lock held
  510. */
  511. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  512. unsigned long address,
  513. unsigned int pages)
  514. {
  515. address >>= PAGE_SHIFT;
  516. iommu_area_free(dom->bitmap, address, pages);
  517. if (address >= dom->next_bit)
  518. dom->need_flush = true;
  519. }
  520. /****************************************************************************
  521. *
  522. * The next functions belong to the domain allocation. A domain is
  523. * allocated for every IOMMU as the default domain. If device isolation
  524. * is enabled, every device get its own domain. The most important thing
  525. * about domains is the page table mapping the DMA address space they
  526. * contain.
  527. *
  528. ****************************************************************************/
  529. static u16 domain_id_alloc(void)
  530. {
  531. unsigned long flags;
  532. int id;
  533. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  534. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  535. BUG_ON(id == 0);
  536. if (id > 0 && id < MAX_DOMAIN_ID)
  537. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  538. else
  539. id = 0;
  540. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  541. return id;
  542. }
  543. #ifdef CONFIG_IOMMU_API
  544. static void domain_id_free(int id)
  545. {
  546. unsigned long flags;
  547. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  548. if (id > 0 && id < MAX_DOMAIN_ID)
  549. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  550. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  551. }
  552. #endif
  553. /*
  554. * Used to reserve address ranges in the aperture (e.g. for exclusion
  555. * ranges.
  556. */
  557. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  558. unsigned long start_page,
  559. unsigned int pages)
  560. {
  561. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  562. if (start_page + pages > last_page)
  563. pages = last_page - start_page;
  564. iommu_area_reserve(dom->bitmap, start_page, pages);
  565. }
  566. static void free_pagetable(struct protection_domain *domain)
  567. {
  568. int i, j;
  569. u64 *p1, *p2, *p3;
  570. p1 = domain->pt_root;
  571. if (!p1)
  572. return;
  573. for (i = 0; i < 512; ++i) {
  574. if (!IOMMU_PTE_PRESENT(p1[i]))
  575. continue;
  576. p2 = IOMMU_PTE_PAGE(p1[i]);
  577. for (j = 0; j < 512; ++j) {
  578. if (!IOMMU_PTE_PRESENT(p2[j]))
  579. continue;
  580. p3 = IOMMU_PTE_PAGE(p2[j]);
  581. free_page((unsigned long)p3);
  582. }
  583. free_page((unsigned long)p2);
  584. }
  585. free_page((unsigned long)p1);
  586. domain->pt_root = NULL;
  587. }
  588. /*
  589. * Free a domain, only used if something went wrong in the
  590. * allocation path and we need to free an already allocated page table
  591. */
  592. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  593. {
  594. if (!dom)
  595. return;
  596. free_pagetable(&dom->domain);
  597. kfree(dom->pte_pages);
  598. kfree(dom->bitmap);
  599. kfree(dom);
  600. }
  601. /*
  602. * Allocates a new protection domain usable for the dma_ops functions.
  603. * It also intializes the page table and the address allocator data
  604. * structures required for the dma_ops interface
  605. */
  606. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  607. unsigned order)
  608. {
  609. struct dma_ops_domain *dma_dom;
  610. unsigned i, num_pte_pages;
  611. u64 *l2_pde;
  612. u64 address;
  613. /*
  614. * Currently the DMA aperture must be between 32 MB and 1GB in size
  615. */
  616. if ((order < 25) || (order > 30))
  617. return NULL;
  618. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  619. if (!dma_dom)
  620. return NULL;
  621. spin_lock_init(&dma_dom->domain.lock);
  622. dma_dom->domain.id = domain_id_alloc();
  623. if (dma_dom->domain.id == 0)
  624. goto free_dma_dom;
  625. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  626. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  627. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  628. dma_dom->domain.priv = dma_dom;
  629. if (!dma_dom->domain.pt_root)
  630. goto free_dma_dom;
  631. dma_dom->aperture_size = (1ULL << order);
  632. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  633. GFP_KERNEL);
  634. if (!dma_dom->bitmap)
  635. goto free_dma_dom;
  636. /*
  637. * mark the first page as allocated so we never return 0 as
  638. * a valid dma-address. So we can use 0 as error value
  639. */
  640. dma_dom->bitmap[0] = 1;
  641. dma_dom->next_bit = 0;
  642. dma_dom->need_flush = false;
  643. dma_dom->target_dev = 0xffff;
  644. /* Intialize the exclusion range if necessary */
  645. if (iommu->exclusion_start &&
  646. iommu->exclusion_start < dma_dom->aperture_size) {
  647. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  648. int pages = iommu_num_pages(iommu->exclusion_start,
  649. iommu->exclusion_length,
  650. PAGE_SIZE);
  651. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  652. }
  653. /*
  654. * At the last step, build the page tables so we don't need to
  655. * allocate page table pages in the dma_ops mapping/unmapping
  656. * path.
  657. */
  658. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  659. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  660. GFP_KERNEL);
  661. if (!dma_dom->pte_pages)
  662. goto free_dma_dom;
  663. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  664. if (l2_pde == NULL)
  665. goto free_dma_dom;
  666. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  667. for (i = 0; i < num_pte_pages; ++i) {
  668. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  669. if (!dma_dom->pte_pages[i])
  670. goto free_dma_dom;
  671. address = virt_to_phys(dma_dom->pte_pages[i]);
  672. l2_pde[i] = IOMMU_L1_PDE(address);
  673. }
  674. return dma_dom;
  675. free_dma_dom:
  676. dma_ops_domain_free(dma_dom);
  677. return NULL;
  678. }
  679. /*
  680. * little helper function to check whether a given protection domain is a
  681. * dma_ops domain
  682. */
  683. static bool dma_ops_domain(struct protection_domain *domain)
  684. {
  685. return domain->flags & PD_DMA_OPS_MASK;
  686. }
  687. /*
  688. * Find out the protection domain structure for a given PCI device. This
  689. * will give us the pointer to the page table root for example.
  690. */
  691. static struct protection_domain *domain_for_device(u16 devid)
  692. {
  693. struct protection_domain *dom;
  694. unsigned long flags;
  695. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  696. dom = amd_iommu_pd_table[devid];
  697. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  698. return dom;
  699. }
  700. /*
  701. * If a device is not yet associated with a domain, this function does
  702. * assigns it visible for the hardware
  703. */
  704. static void attach_device(struct amd_iommu *iommu,
  705. struct protection_domain *domain,
  706. u16 devid)
  707. {
  708. unsigned long flags;
  709. u64 pte_root = virt_to_phys(domain->pt_root);
  710. domain->dev_cnt += 1;
  711. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  712. << DEV_ENTRY_MODE_SHIFT;
  713. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  714. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  715. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  716. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  717. amd_iommu_dev_table[devid].data[2] = domain->id;
  718. amd_iommu_pd_table[devid] = domain;
  719. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  720. iommu_queue_inv_dev_entry(iommu, devid);
  721. }
  722. /*
  723. * Removes a device from a protection domain (unlocked)
  724. */
  725. static void __detach_device(struct protection_domain *domain, u16 devid)
  726. {
  727. /* lock domain */
  728. spin_lock(&domain->lock);
  729. /* remove domain from the lookup table */
  730. amd_iommu_pd_table[devid] = NULL;
  731. /* remove entry from the device table seen by the hardware */
  732. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  733. amd_iommu_dev_table[devid].data[1] = 0;
  734. amd_iommu_dev_table[devid].data[2] = 0;
  735. /* decrease reference counter */
  736. domain->dev_cnt -= 1;
  737. /* ready */
  738. spin_unlock(&domain->lock);
  739. }
  740. /*
  741. * Removes a device from a protection domain (with devtable_lock held)
  742. */
  743. static void detach_device(struct protection_domain *domain, u16 devid)
  744. {
  745. unsigned long flags;
  746. /* lock device table */
  747. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  748. __detach_device(domain, devid);
  749. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  750. }
  751. static int device_change_notifier(struct notifier_block *nb,
  752. unsigned long action, void *data)
  753. {
  754. struct device *dev = data;
  755. struct pci_dev *pdev = to_pci_dev(dev);
  756. u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
  757. struct protection_domain *domain;
  758. struct dma_ops_domain *dma_domain;
  759. struct amd_iommu *iommu;
  760. if (devid > amd_iommu_last_bdf)
  761. goto out;
  762. devid = amd_iommu_alias_table[devid];
  763. iommu = amd_iommu_rlookup_table[devid];
  764. if (iommu == NULL)
  765. goto out;
  766. domain = domain_for_device(devid);
  767. if (domain && !dma_ops_domain(domain))
  768. WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
  769. "to a non-dma-ops domain\n", dev_name(dev));
  770. switch (action) {
  771. case BUS_NOTIFY_BOUND_DRIVER:
  772. if (domain)
  773. goto out;
  774. dma_domain = find_protection_domain(devid);
  775. if (!dma_domain)
  776. dma_domain = iommu->default_dom;
  777. attach_device(iommu, &dma_domain->domain, devid);
  778. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  779. "device %s\n", dma_domain->domain.id, dev_name(dev));
  780. break;
  781. case BUS_NOTIFY_UNBIND_DRIVER:
  782. if (!domain)
  783. goto out;
  784. detach_device(domain, devid);
  785. break;
  786. default:
  787. goto out;
  788. }
  789. iommu_queue_inv_dev_entry(iommu, devid);
  790. iommu_completion_wait(iommu);
  791. out:
  792. return 0;
  793. }
  794. struct notifier_block device_nb = {
  795. .notifier_call = device_change_notifier,
  796. };
  797. /*****************************************************************************
  798. *
  799. * The next functions belong to the dma_ops mapping/unmapping code.
  800. *
  801. *****************************************************************************/
  802. /*
  803. * This function checks if the driver got a valid device from the caller to
  804. * avoid dereferencing invalid pointers.
  805. */
  806. static bool check_device(struct device *dev)
  807. {
  808. if (!dev || !dev->dma_mask)
  809. return false;
  810. return true;
  811. }
  812. /*
  813. * In this function the list of preallocated protection domains is traversed to
  814. * find the domain for a specific device
  815. */
  816. static struct dma_ops_domain *find_protection_domain(u16 devid)
  817. {
  818. struct dma_ops_domain *entry, *ret = NULL;
  819. unsigned long flags;
  820. if (list_empty(&iommu_pd_list))
  821. return NULL;
  822. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  823. list_for_each_entry(entry, &iommu_pd_list, list) {
  824. if (entry->target_dev == devid) {
  825. ret = entry;
  826. break;
  827. }
  828. }
  829. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  830. return ret;
  831. }
  832. /*
  833. * In the dma_ops path we only have the struct device. This function
  834. * finds the corresponding IOMMU, the protection domain and the
  835. * requestor id for a given device.
  836. * If the device is not yet associated with a domain this is also done
  837. * in this function.
  838. */
  839. static int get_device_resources(struct device *dev,
  840. struct amd_iommu **iommu,
  841. struct protection_domain **domain,
  842. u16 *bdf)
  843. {
  844. struct dma_ops_domain *dma_dom;
  845. struct pci_dev *pcidev;
  846. u16 _bdf;
  847. *iommu = NULL;
  848. *domain = NULL;
  849. *bdf = 0xffff;
  850. if (dev->bus != &pci_bus_type)
  851. return 0;
  852. pcidev = to_pci_dev(dev);
  853. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  854. /* device not translated by any IOMMU in the system? */
  855. if (_bdf > amd_iommu_last_bdf)
  856. return 0;
  857. *bdf = amd_iommu_alias_table[_bdf];
  858. *iommu = amd_iommu_rlookup_table[*bdf];
  859. if (*iommu == NULL)
  860. return 0;
  861. *domain = domain_for_device(*bdf);
  862. if (*domain == NULL) {
  863. dma_dom = find_protection_domain(*bdf);
  864. if (!dma_dom)
  865. dma_dom = (*iommu)->default_dom;
  866. *domain = &dma_dom->domain;
  867. attach_device(*iommu, *domain, *bdf);
  868. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  869. "device ", (*domain)->id);
  870. print_devid(_bdf, 1);
  871. }
  872. if (domain_for_device(_bdf) == NULL)
  873. attach_device(*iommu, *domain, _bdf);
  874. return 1;
  875. }
  876. /*
  877. * This is the generic map function. It maps one 4kb page at paddr to
  878. * the given address in the DMA address space for the domain.
  879. */
  880. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  881. struct dma_ops_domain *dom,
  882. unsigned long address,
  883. phys_addr_t paddr,
  884. int direction)
  885. {
  886. u64 *pte, __pte;
  887. WARN_ON(address > dom->aperture_size);
  888. paddr &= PAGE_MASK;
  889. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  890. pte += IOMMU_PTE_L0_INDEX(address);
  891. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  892. if (direction == DMA_TO_DEVICE)
  893. __pte |= IOMMU_PTE_IR;
  894. else if (direction == DMA_FROM_DEVICE)
  895. __pte |= IOMMU_PTE_IW;
  896. else if (direction == DMA_BIDIRECTIONAL)
  897. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  898. WARN_ON(*pte);
  899. *pte = __pte;
  900. return (dma_addr_t)address;
  901. }
  902. /*
  903. * The generic unmapping function for on page in the DMA address space.
  904. */
  905. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  906. struct dma_ops_domain *dom,
  907. unsigned long address)
  908. {
  909. u64 *pte;
  910. if (address >= dom->aperture_size)
  911. return;
  912. WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
  913. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  914. pte += IOMMU_PTE_L0_INDEX(address);
  915. WARN_ON(!*pte);
  916. *pte = 0ULL;
  917. }
  918. /*
  919. * This function contains common code for mapping of a physically
  920. * contiguous memory region into DMA address space. It is used by all
  921. * mapping functions provided with this IOMMU driver.
  922. * Must be called with the domain lock held.
  923. */
  924. static dma_addr_t __map_single(struct device *dev,
  925. struct amd_iommu *iommu,
  926. struct dma_ops_domain *dma_dom,
  927. phys_addr_t paddr,
  928. size_t size,
  929. int dir,
  930. bool align,
  931. u64 dma_mask)
  932. {
  933. dma_addr_t offset = paddr & ~PAGE_MASK;
  934. dma_addr_t address, start;
  935. unsigned int pages;
  936. unsigned long align_mask = 0;
  937. int i;
  938. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  939. paddr &= PAGE_MASK;
  940. if (align)
  941. align_mask = (1UL << get_order(size)) - 1;
  942. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  943. dma_mask);
  944. if (unlikely(address == bad_dma_address))
  945. goto out;
  946. start = address;
  947. for (i = 0; i < pages; ++i) {
  948. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  949. paddr += PAGE_SIZE;
  950. start += PAGE_SIZE;
  951. }
  952. address += offset;
  953. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  954. iommu_flush_tlb(iommu, dma_dom->domain.id);
  955. dma_dom->need_flush = false;
  956. } else if (unlikely(iommu_has_npcache(iommu)))
  957. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  958. out:
  959. return address;
  960. }
  961. /*
  962. * Does the reverse of the __map_single function. Must be called with
  963. * the domain lock held too
  964. */
  965. static void __unmap_single(struct amd_iommu *iommu,
  966. struct dma_ops_domain *dma_dom,
  967. dma_addr_t dma_addr,
  968. size_t size,
  969. int dir)
  970. {
  971. dma_addr_t i, start;
  972. unsigned int pages;
  973. if ((dma_addr == bad_dma_address) ||
  974. (dma_addr + size > dma_dom->aperture_size))
  975. return;
  976. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  977. dma_addr &= PAGE_MASK;
  978. start = dma_addr;
  979. for (i = 0; i < pages; ++i) {
  980. dma_ops_domain_unmap(iommu, dma_dom, start);
  981. start += PAGE_SIZE;
  982. }
  983. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  984. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  985. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  986. dma_dom->need_flush = false;
  987. }
  988. }
  989. /*
  990. * The exported map_single function for dma_ops.
  991. */
  992. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  993. size_t size, int dir)
  994. {
  995. unsigned long flags;
  996. struct amd_iommu *iommu;
  997. struct protection_domain *domain;
  998. u16 devid;
  999. dma_addr_t addr;
  1000. u64 dma_mask;
  1001. if (!check_device(dev))
  1002. return bad_dma_address;
  1003. dma_mask = *dev->dma_mask;
  1004. get_device_resources(dev, &iommu, &domain, &devid);
  1005. if (iommu == NULL || domain == NULL)
  1006. /* device not handled by any AMD IOMMU */
  1007. return (dma_addr_t)paddr;
  1008. if (!dma_ops_domain(domain))
  1009. return bad_dma_address;
  1010. spin_lock_irqsave(&domain->lock, flags);
  1011. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  1012. dma_mask);
  1013. if (addr == bad_dma_address)
  1014. goto out;
  1015. iommu_completion_wait(iommu);
  1016. out:
  1017. spin_unlock_irqrestore(&domain->lock, flags);
  1018. return addr;
  1019. }
  1020. /*
  1021. * The exported unmap_single function for dma_ops.
  1022. */
  1023. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  1024. size_t size, int dir)
  1025. {
  1026. unsigned long flags;
  1027. struct amd_iommu *iommu;
  1028. struct protection_domain *domain;
  1029. u16 devid;
  1030. if (!check_device(dev) ||
  1031. !get_device_resources(dev, &iommu, &domain, &devid))
  1032. /* device not handled by any AMD IOMMU */
  1033. return;
  1034. if (!dma_ops_domain(domain))
  1035. return;
  1036. spin_lock_irqsave(&domain->lock, flags);
  1037. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  1038. iommu_completion_wait(iommu);
  1039. spin_unlock_irqrestore(&domain->lock, flags);
  1040. }
  1041. /*
  1042. * This is a special map_sg function which is used if we should map a
  1043. * device which is not handled by an AMD IOMMU in the system.
  1044. */
  1045. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1046. int nelems, int dir)
  1047. {
  1048. struct scatterlist *s;
  1049. int i;
  1050. for_each_sg(sglist, s, nelems, i) {
  1051. s->dma_address = (dma_addr_t)sg_phys(s);
  1052. s->dma_length = s->length;
  1053. }
  1054. return nelems;
  1055. }
  1056. /*
  1057. * The exported map_sg function for dma_ops (handles scatter-gather
  1058. * lists).
  1059. */
  1060. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1061. int nelems, int dir)
  1062. {
  1063. unsigned long flags;
  1064. struct amd_iommu *iommu;
  1065. struct protection_domain *domain;
  1066. u16 devid;
  1067. int i;
  1068. struct scatterlist *s;
  1069. phys_addr_t paddr;
  1070. int mapped_elems = 0;
  1071. u64 dma_mask;
  1072. if (!check_device(dev))
  1073. return 0;
  1074. dma_mask = *dev->dma_mask;
  1075. get_device_resources(dev, &iommu, &domain, &devid);
  1076. if (!iommu || !domain)
  1077. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1078. if (!dma_ops_domain(domain))
  1079. return 0;
  1080. spin_lock_irqsave(&domain->lock, flags);
  1081. for_each_sg(sglist, s, nelems, i) {
  1082. paddr = sg_phys(s);
  1083. s->dma_address = __map_single(dev, iommu, domain->priv,
  1084. paddr, s->length, dir, false,
  1085. dma_mask);
  1086. if (s->dma_address) {
  1087. s->dma_length = s->length;
  1088. mapped_elems++;
  1089. } else
  1090. goto unmap;
  1091. }
  1092. iommu_completion_wait(iommu);
  1093. out:
  1094. spin_unlock_irqrestore(&domain->lock, flags);
  1095. return mapped_elems;
  1096. unmap:
  1097. for_each_sg(sglist, s, mapped_elems, i) {
  1098. if (s->dma_address)
  1099. __unmap_single(iommu, domain->priv, s->dma_address,
  1100. s->dma_length, dir);
  1101. s->dma_address = s->dma_length = 0;
  1102. }
  1103. mapped_elems = 0;
  1104. goto out;
  1105. }
  1106. /*
  1107. * The exported map_sg function for dma_ops (handles scatter-gather
  1108. * lists).
  1109. */
  1110. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1111. int nelems, int dir)
  1112. {
  1113. unsigned long flags;
  1114. struct amd_iommu *iommu;
  1115. struct protection_domain *domain;
  1116. struct scatterlist *s;
  1117. u16 devid;
  1118. int i;
  1119. if (!check_device(dev) ||
  1120. !get_device_resources(dev, &iommu, &domain, &devid))
  1121. return;
  1122. if (!dma_ops_domain(domain))
  1123. return;
  1124. spin_lock_irqsave(&domain->lock, flags);
  1125. for_each_sg(sglist, s, nelems, i) {
  1126. __unmap_single(iommu, domain->priv, s->dma_address,
  1127. s->dma_length, dir);
  1128. s->dma_address = s->dma_length = 0;
  1129. }
  1130. iommu_completion_wait(iommu);
  1131. spin_unlock_irqrestore(&domain->lock, flags);
  1132. }
  1133. /*
  1134. * The exported alloc_coherent function for dma_ops.
  1135. */
  1136. static void *alloc_coherent(struct device *dev, size_t size,
  1137. dma_addr_t *dma_addr, gfp_t flag)
  1138. {
  1139. unsigned long flags;
  1140. void *virt_addr;
  1141. struct amd_iommu *iommu;
  1142. struct protection_domain *domain;
  1143. u16 devid;
  1144. phys_addr_t paddr;
  1145. u64 dma_mask = dev->coherent_dma_mask;
  1146. if (!check_device(dev))
  1147. return NULL;
  1148. if (!get_device_resources(dev, &iommu, &domain, &devid))
  1149. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1150. flag |= __GFP_ZERO;
  1151. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1152. if (!virt_addr)
  1153. return 0;
  1154. paddr = virt_to_phys(virt_addr);
  1155. if (!iommu || !domain) {
  1156. *dma_addr = (dma_addr_t)paddr;
  1157. return virt_addr;
  1158. }
  1159. if (!dma_ops_domain(domain))
  1160. goto out_free;
  1161. if (!dma_mask)
  1162. dma_mask = *dev->dma_mask;
  1163. spin_lock_irqsave(&domain->lock, flags);
  1164. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  1165. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1166. if (*dma_addr == bad_dma_address)
  1167. goto out_free;
  1168. iommu_completion_wait(iommu);
  1169. spin_unlock_irqrestore(&domain->lock, flags);
  1170. return virt_addr;
  1171. out_free:
  1172. free_pages((unsigned long)virt_addr, get_order(size));
  1173. return NULL;
  1174. }
  1175. /*
  1176. * The exported free_coherent function for dma_ops.
  1177. */
  1178. static void free_coherent(struct device *dev, size_t size,
  1179. void *virt_addr, dma_addr_t dma_addr)
  1180. {
  1181. unsigned long flags;
  1182. struct amd_iommu *iommu;
  1183. struct protection_domain *domain;
  1184. u16 devid;
  1185. if (!check_device(dev))
  1186. return;
  1187. get_device_resources(dev, &iommu, &domain, &devid);
  1188. if (!iommu || !domain)
  1189. goto free_mem;
  1190. if (!dma_ops_domain(domain))
  1191. goto free_mem;
  1192. spin_lock_irqsave(&domain->lock, flags);
  1193. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1194. iommu_completion_wait(iommu);
  1195. spin_unlock_irqrestore(&domain->lock, flags);
  1196. free_mem:
  1197. free_pages((unsigned long)virt_addr, get_order(size));
  1198. }
  1199. /*
  1200. * This function is called by the DMA layer to find out if we can handle a
  1201. * particular device. It is part of the dma_ops.
  1202. */
  1203. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1204. {
  1205. u16 bdf;
  1206. struct pci_dev *pcidev;
  1207. /* No device or no PCI device */
  1208. if (!dev || dev->bus != &pci_bus_type)
  1209. return 0;
  1210. pcidev = to_pci_dev(dev);
  1211. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1212. /* Out of our scope? */
  1213. if (bdf > amd_iommu_last_bdf)
  1214. return 0;
  1215. return 1;
  1216. }
  1217. /*
  1218. * The function for pre-allocating protection domains.
  1219. *
  1220. * If the driver core informs the DMA layer if a driver grabs a device
  1221. * we don't need to preallocate the protection domains anymore.
  1222. * For now we have to.
  1223. */
  1224. void prealloc_protection_domains(void)
  1225. {
  1226. struct pci_dev *dev = NULL;
  1227. struct dma_ops_domain *dma_dom;
  1228. struct amd_iommu *iommu;
  1229. int order = amd_iommu_aperture_order;
  1230. u16 devid;
  1231. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1232. devid = (dev->bus->number << 8) | dev->devfn;
  1233. if (devid > amd_iommu_last_bdf)
  1234. continue;
  1235. devid = amd_iommu_alias_table[devid];
  1236. if (domain_for_device(devid))
  1237. continue;
  1238. iommu = amd_iommu_rlookup_table[devid];
  1239. if (!iommu)
  1240. continue;
  1241. dma_dom = dma_ops_domain_alloc(iommu, order);
  1242. if (!dma_dom)
  1243. continue;
  1244. init_unity_mappings_for_device(dma_dom, devid);
  1245. dma_dom->target_dev = devid;
  1246. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1247. }
  1248. }
  1249. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1250. .alloc_coherent = alloc_coherent,
  1251. .free_coherent = free_coherent,
  1252. .map_single = map_single,
  1253. .unmap_single = unmap_single,
  1254. .map_sg = map_sg,
  1255. .unmap_sg = unmap_sg,
  1256. .dma_supported = amd_iommu_dma_supported,
  1257. };
  1258. /*
  1259. * The function which clues the AMD IOMMU driver into dma_ops.
  1260. */
  1261. int __init amd_iommu_init_dma_ops(void)
  1262. {
  1263. struct amd_iommu *iommu;
  1264. int order = amd_iommu_aperture_order;
  1265. int ret;
  1266. /*
  1267. * first allocate a default protection domain for every IOMMU we
  1268. * found in the system. Devices not assigned to any other
  1269. * protection domain will be assigned to the default one.
  1270. */
  1271. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1272. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1273. if (iommu->default_dom == NULL)
  1274. return -ENOMEM;
  1275. ret = iommu_init_unity_mappings(iommu);
  1276. if (ret)
  1277. goto free_domains;
  1278. }
  1279. /*
  1280. * If device isolation is enabled, pre-allocate the protection
  1281. * domains for each device.
  1282. */
  1283. if (amd_iommu_isolate)
  1284. prealloc_protection_domains();
  1285. iommu_detected = 1;
  1286. force_iommu = 1;
  1287. bad_dma_address = 0;
  1288. #ifdef CONFIG_GART_IOMMU
  1289. gart_iommu_aperture_disabled = 1;
  1290. gart_iommu_aperture = 0;
  1291. #endif
  1292. /* Make the driver finally visible to the drivers */
  1293. dma_ops = &amd_iommu_dma_ops;
  1294. bus_register_notifier(&pci_bus_type, &device_nb);
  1295. return 0;
  1296. free_domains:
  1297. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1298. if (iommu->default_dom)
  1299. dma_ops_domain_free(iommu->default_dom);
  1300. }
  1301. return ret;
  1302. }
  1303. /*****************************************************************************
  1304. *
  1305. * The following functions belong to the exported interface of AMD IOMMU
  1306. *
  1307. * This interface allows access to lower level functions of the IOMMU
  1308. * like protection domain handling and assignement of devices to domains
  1309. * which is not possible with the dma_ops interface.
  1310. *
  1311. *****************************************************************************/
  1312. #ifdef CONFIG_IOMMU_API
  1313. static void cleanup_domain(struct protection_domain *domain)
  1314. {
  1315. unsigned long flags;
  1316. u16 devid;
  1317. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1318. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1319. if (amd_iommu_pd_table[devid] == domain)
  1320. __detach_device(domain, devid);
  1321. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1322. }
  1323. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1324. {
  1325. struct protection_domain *domain;
  1326. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1327. if (!domain)
  1328. return -ENOMEM;
  1329. spin_lock_init(&domain->lock);
  1330. domain->mode = PAGE_MODE_3_LEVEL;
  1331. domain->id = domain_id_alloc();
  1332. if (!domain->id)
  1333. goto out_free;
  1334. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1335. if (!domain->pt_root)
  1336. goto out_free;
  1337. dom->priv = domain;
  1338. return 0;
  1339. out_free:
  1340. kfree(domain);
  1341. return -ENOMEM;
  1342. }
  1343. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1344. {
  1345. struct protection_domain *domain = dom->priv;
  1346. if (!domain)
  1347. return;
  1348. if (domain->dev_cnt > 0)
  1349. cleanup_domain(domain);
  1350. BUG_ON(domain->dev_cnt != 0);
  1351. free_pagetable(domain);
  1352. domain_id_free(domain->id);
  1353. kfree(domain);
  1354. dom->priv = NULL;
  1355. }
  1356. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1357. struct device *dev)
  1358. {
  1359. struct protection_domain *domain = dom->priv;
  1360. struct amd_iommu *iommu;
  1361. struct pci_dev *pdev;
  1362. u16 devid;
  1363. if (dev->bus != &pci_bus_type)
  1364. return;
  1365. pdev = to_pci_dev(dev);
  1366. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1367. if (devid > 0)
  1368. detach_device(domain, devid);
  1369. iommu = amd_iommu_rlookup_table[devid];
  1370. if (!iommu)
  1371. return;
  1372. iommu_queue_inv_dev_entry(iommu, devid);
  1373. iommu_completion_wait(iommu);
  1374. }
  1375. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1376. struct device *dev)
  1377. {
  1378. struct protection_domain *domain = dom->priv;
  1379. struct protection_domain *old_domain;
  1380. struct amd_iommu *iommu;
  1381. struct pci_dev *pdev;
  1382. u16 devid;
  1383. if (dev->bus != &pci_bus_type)
  1384. return -EINVAL;
  1385. pdev = to_pci_dev(dev);
  1386. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1387. if (devid >= amd_iommu_last_bdf ||
  1388. devid != amd_iommu_alias_table[devid])
  1389. return -EINVAL;
  1390. iommu = amd_iommu_rlookup_table[devid];
  1391. if (!iommu)
  1392. return -EINVAL;
  1393. old_domain = domain_for_device(devid);
  1394. if (old_domain)
  1395. return -EBUSY;
  1396. attach_device(iommu, domain, devid);
  1397. iommu_completion_wait(iommu);
  1398. return 0;
  1399. }
  1400. static int amd_iommu_map_range(struct iommu_domain *dom,
  1401. unsigned long iova, phys_addr_t paddr,
  1402. size_t size, int iommu_prot)
  1403. {
  1404. struct protection_domain *domain = dom->priv;
  1405. unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1406. int prot = 0;
  1407. int ret;
  1408. if (iommu_prot & IOMMU_READ)
  1409. prot |= IOMMU_PROT_IR;
  1410. if (iommu_prot & IOMMU_WRITE)
  1411. prot |= IOMMU_PROT_IW;
  1412. iova &= PAGE_MASK;
  1413. paddr &= PAGE_MASK;
  1414. for (i = 0; i < npages; ++i) {
  1415. ret = iommu_map_page(domain, iova, paddr, prot);
  1416. if (ret)
  1417. return ret;
  1418. iova += PAGE_SIZE;
  1419. paddr += PAGE_SIZE;
  1420. }
  1421. return 0;
  1422. }
  1423. static void amd_iommu_unmap_range(struct iommu_domain *dom,
  1424. unsigned long iova, size_t size)
  1425. {
  1426. struct protection_domain *domain = dom->priv;
  1427. unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
  1428. iova &= PAGE_MASK;
  1429. for (i = 0; i < npages; ++i) {
  1430. iommu_unmap_page(domain, iova);
  1431. iova += PAGE_SIZE;
  1432. }
  1433. iommu_flush_domain(domain->id);
  1434. }
  1435. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1436. unsigned long iova)
  1437. {
  1438. struct protection_domain *domain = dom->priv;
  1439. unsigned long offset = iova & ~PAGE_MASK;
  1440. phys_addr_t paddr;
  1441. u64 *pte;
  1442. pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)];
  1443. if (!IOMMU_PTE_PRESENT(*pte))
  1444. return 0;
  1445. pte = IOMMU_PTE_PAGE(*pte);
  1446. pte = &pte[IOMMU_PTE_L1_INDEX(iova)];
  1447. if (!IOMMU_PTE_PRESENT(*pte))
  1448. return 0;
  1449. pte = IOMMU_PTE_PAGE(*pte);
  1450. pte = &pte[IOMMU_PTE_L0_INDEX(iova)];
  1451. if (!IOMMU_PTE_PRESENT(*pte))
  1452. return 0;
  1453. paddr = *pte & IOMMU_PAGE_MASK;
  1454. paddr |= offset;
  1455. return paddr;
  1456. }
  1457. #endif