amd_iommu.c 51 KB

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