amd_iommu.c 65 KB

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