amd_iommu.c 64 KB

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