amd_iommu.c 51 KB

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