amd_iommu.c 53 KB

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