amd_iommu.c 63 KB

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