amd_iommu.c 51 KB

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