amd_iommu.c 53 KB

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