amd_iommu.c 57 KB

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