amd_iommu.c 64 KB

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