intel-iommu.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241
  1. /*
  2. * Copyright (c) 2006, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Copyright (C) 2006-2008 Intel Corporation
  18. * Author: Ashok Raj <ashok.raj@intel.com>
  19. * Author: Shaohua Li <shaohua.li@intel.com>
  20. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  21. * Author: Fenghua Yu <fenghua.yu@intel.com>
  22. */
  23. #include <linux/init.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/slab.h>
  27. #include <linux/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/pci.h>
  31. #include <linux/dmar.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/mempool.h>
  34. #include <linux/timer.h>
  35. #include <linux/iova.h>
  36. #include <linux/iommu.h>
  37. #include <linux/intel-iommu.h>
  38. #include <linux/sysdev.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/iommu.h>
  41. #include "pci.h"
  42. #define ROOT_SIZE VTD_PAGE_SIZE
  43. #define CONTEXT_SIZE VTD_PAGE_SIZE
  44. #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  45. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  46. #define IOAPIC_RANGE_START (0xfee00000)
  47. #define IOAPIC_RANGE_END (0xfeefffff)
  48. #define IOVA_START_ADDR (0x1000)
  49. #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
  50. #define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
  51. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  52. #define DMA_32BIT_PFN IOVA_PFN(DMA_32BIT_MASK)
  53. #define DMA_64BIT_PFN IOVA_PFN(DMA_64BIT_MASK)
  54. /* global iommu list, set NULL for ignored DMAR units */
  55. static struct intel_iommu **g_iommus;
  56. static int rwbf_quirk;
  57. /*
  58. * 0: Present
  59. * 1-11: Reserved
  60. * 12-63: Context Ptr (12 - (haw-1))
  61. * 64-127: Reserved
  62. */
  63. struct root_entry {
  64. u64 val;
  65. u64 rsvd1;
  66. };
  67. #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
  68. static inline bool root_present(struct root_entry *root)
  69. {
  70. return (root->val & 1);
  71. }
  72. static inline void set_root_present(struct root_entry *root)
  73. {
  74. root->val |= 1;
  75. }
  76. static inline void set_root_value(struct root_entry *root, unsigned long value)
  77. {
  78. root->val |= value & VTD_PAGE_MASK;
  79. }
  80. static inline struct context_entry *
  81. get_context_addr_from_root(struct root_entry *root)
  82. {
  83. return (struct context_entry *)
  84. (root_present(root)?phys_to_virt(
  85. root->val & VTD_PAGE_MASK) :
  86. NULL);
  87. }
  88. /*
  89. * low 64 bits:
  90. * 0: present
  91. * 1: fault processing disable
  92. * 2-3: translation type
  93. * 12-63: address space root
  94. * high 64 bits:
  95. * 0-2: address width
  96. * 3-6: aval
  97. * 8-23: domain id
  98. */
  99. struct context_entry {
  100. u64 lo;
  101. u64 hi;
  102. };
  103. static inline bool context_present(struct context_entry *context)
  104. {
  105. return (context->lo & 1);
  106. }
  107. static inline void context_set_present(struct context_entry *context)
  108. {
  109. context->lo |= 1;
  110. }
  111. static inline void context_set_fault_enable(struct context_entry *context)
  112. {
  113. context->lo &= (((u64)-1) << 2) | 1;
  114. }
  115. #define CONTEXT_TT_MULTI_LEVEL 0
  116. static inline void context_set_translation_type(struct context_entry *context,
  117. unsigned long value)
  118. {
  119. context->lo &= (((u64)-1) << 4) | 3;
  120. context->lo |= (value & 3) << 2;
  121. }
  122. static inline void context_set_address_root(struct context_entry *context,
  123. unsigned long value)
  124. {
  125. context->lo |= value & VTD_PAGE_MASK;
  126. }
  127. static inline void context_set_address_width(struct context_entry *context,
  128. unsigned long value)
  129. {
  130. context->hi |= value & 7;
  131. }
  132. static inline void context_set_domain_id(struct context_entry *context,
  133. unsigned long value)
  134. {
  135. context->hi |= (value & ((1 << 16) - 1)) << 8;
  136. }
  137. static inline void context_clear_entry(struct context_entry *context)
  138. {
  139. context->lo = 0;
  140. context->hi = 0;
  141. }
  142. /*
  143. * 0: readable
  144. * 1: writable
  145. * 2-6: reserved
  146. * 7: super page
  147. * 8-10: available
  148. * 11: snoop behavior
  149. * 12-63: Host physcial address
  150. */
  151. struct dma_pte {
  152. u64 val;
  153. };
  154. static inline void dma_clear_pte(struct dma_pte *pte)
  155. {
  156. pte->val = 0;
  157. }
  158. static inline void dma_set_pte_readable(struct dma_pte *pte)
  159. {
  160. pte->val |= DMA_PTE_READ;
  161. }
  162. static inline void dma_set_pte_writable(struct dma_pte *pte)
  163. {
  164. pte->val |= DMA_PTE_WRITE;
  165. }
  166. static inline void dma_set_pte_snp(struct dma_pte *pte)
  167. {
  168. pte->val |= DMA_PTE_SNP;
  169. }
  170. static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
  171. {
  172. pte->val = (pte->val & ~3) | (prot & 3);
  173. }
  174. static inline u64 dma_pte_addr(struct dma_pte *pte)
  175. {
  176. return (pte->val & VTD_PAGE_MASK);
  177. }
  178. static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
  179. {
  180. pte->val |= (addr & VTD_PAGE_MASK);
  181. }
  182. static inline bool dma_pte_present(struct dma_pte *pte)
  183. {
  184. return (pte->val & 3) != 0;
  185. }
  186. /* devices under the same p2p bridge are owned in one domain */
  187. #define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
  188. /* domain represents a virtual machine, more than one devices
  189. * across iommus may be owned in one domain, e.g. kvm guest.
  190. */
  191. #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
  192. struct dmar_domain {
  193. int id; /* domain id */
  194. unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
  195. struct list_head devices; /* all devices' list */
  196. struct iova_domain iovad; /* iova's that belong to this domain */
  197. struct dma_pte *pgd; /* virtual address */
  198. spinlock_t mapping_lock; /* page table lock */
  199. int gaw; /* max guest address width */
  200. /* adjusted guest address width, 0 is level 2 30-bit */
  201. int agaw;
  202. int flags; /* flags to find out type of domain */
  203. int iommu_coherency;/* indicate coherency of iommu access */
  204. int iommu_snooping; /* indicate snooping control feature*/
  205. int iommu_count; /* reference count of iommu */
  206. spinlock_t iommu_lock; /* protect iommu set in domain */
  207. u64 max_addr; /* maximum mapped address */
  208. };
  209. /* PCI domain-device relationship */
  210. struct device_domain_info {
  211. struct list_head link; /* link to domain siblings */
  212. struct list_head global; /* link to global list */
  213. u8 bus; /* PCI bus numer */
  214. u8 devfn; /* PCI devfn number */
  215. struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
  216. struct dmar_domain *domain; /* pointer to domain */
  217. };
  218. static void flush_unmaps_timeout(unsigned long data);
  219. DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
  220. #define HIGH_WATER_MARK 250
  221. struct deferred_flush_tables {
  222. int next;
  223. struct iova *iova[HIGH_WATER_MARK];
  224. struct dmar_domain *domain[HIGH_WATER_MARK];
  225. };
  226. static struct deferred_flush_tables *deferred_flush;
  227. /* bitmap for indexing intel_iommus */
  228. static int g_num_of_iommus;
  229. static DEFINE_SPINLOCK(async_umap_flush_lock);
  230. static LIST_HEAD(unmaps_to_do);
  231. static int timer_on;
  232. static long list_size;
  233. static void domain_remove_dev_info(struct dmar_domain *domain);
  234. #ifdef CONFIG_DMAR_DEFAULT_ON
  235. int dmar_disabled = 0;
  236. #else
  237. int dmar_disabled = 1;
  238. #endif /*CONFIG_DMAR_DEFAULT_ON*/
  239. static int __initdata dmar_map_gfx = 1;
  240. static int dmar_forcedac;
  241. static int intel_iommu_strict;
  242. #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
  243. static DEFINE_SPINLOCK(device_domain_lock);
  244. static LIST_HEAD(device_domain_list);
  245. static struct iommu_ops intel_iommu_ops;
  246. static int __init intel_iommu_setup(char *str)
  247. {
  248. if (!str)
  249. return -EINVAL;
  250. while (*str) {
  251. if (!strncmp(str, "on", 2)) {
  252. dmar_disabled = 0;
  253. printk(KERN_INFO "Intel-IOMMU: enabled\n");
  254. } else if (!strncmp(str, "off", 3)) {
  255. dmar_disabled = 1;
  256. printk(KERN_INFO "Intel-IOMMU: disabled\n");
  257. } else if (!strncmp(str, "igfx_off", 8)) {
  258. dmar_map_gfx = 0;
  259. printk(KERN_INFO
  260. "Intel-IOMMU: disable GFX device mapping\n");
  261. } else if (!strncmp(str, "forcedac", 8)) {
  262. printk(KERN_INFO
  263. "Intel-IOMMU: Forcing DAC for PCI devices\n");
  264. dmar_forcedac = 1;
  265. } else if (!strncmp(str, "strict", 6)) {
  266. printk(KERN_INFO
  267. "Intel-IOMMU: disable batched IOTLB flush\n");
  268. intel_iommu_strict = 1;
  269. }
  270. str += strcspn(str, ",");
  271. while (*str == ',')
  272. str++;
  273. }
  274. return 0;
  275. }
  276. __setup("intel_iommu=", intel_iommu_setup);
  277. static struct kmem_cache *iommu_domain_cache;
  278. static struct kmem_cache *iommu_devinfo_cache;
  279. static struct kmem_cache *iommu_iova_cache;
  280. static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
  281. {
  282. unsigned int flags;
  283. void *vaddr;
  284. /* trying to avoid low memory issues */
  285. flags = current->flags & PF_MEMALLOC;
  286. current->flags |= PF_MEMALLOC;
  287. vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
  288. current->flags &= (~PF_MEMALLOC | flags);
  289. return vaddr;
  290. }
  291. static inline void *alloc_pgtable_page(void)
  292. {
  293. unsigned int flags;
  294. void *vaddr;
  295. /* trying to avoid low memory issues */
  296. flags = current->flags & PF_MEMALLOC;
  297. current->flags |= PF_MEMALLOC;
  298. vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
  299. current->flags &= (~PF_MEMALLOC | flags);
  300. return vaddr;
  301. }
  302. static inline void free_pgtable_page(void *vaddr)
  303. {
  304. free_page((unsigned long)vaddr);
  305. }
  306. static inline void *alloc_domain_mem(void)
  307. {
  308. return iommu_kmem_cache_alloc(iommu_domain_cache);
  309. }
  310. static void free_domain_mem(void *vaddr)
  311. {
  312. kmem_cache_free(iommu_domain_cache, vaddr);
  313. }
  314. static inline void * alloc_devinfo_mem(void)
  315. {
  316. return iommu_kmem_cache_alloc(iommu_devinfo_cache);
  317. }
  318. static inline void free_devinfo_mem(void *vaddr)
  319. {
  320. kmem_cache_free(iommu_devinfo_cache, vaddr);
  321. }
  322. struct iova *alloc_iova_mem(void)
  323. {
  324. return iommu_kmem_cache_alloc(iommu_iova_cache);
  325. }
  326. void free_iova_mem(struct iova *iova)
  327. {
  328. kmem_cache_free(iommu_iova_cache, iova);
  329. }
  330. static inline int width_to_agaw(int width);
  331. /* calculate agaw for each iommu.
  332. * "SAGAW" may be different across iommus, use a default agaw, and
  333. * get a supported less agaw for iommus that don't support the default agaw.
  334. */
  335. int iommu_calculate_agaw(struct intel_iommu *iommu)
  336. {
  337. unsigned long sagaw;
  338. int agaw = -1;
  339. sagaw = cap_sagaw(iommu->cap);
  340. for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
  341. agaw >= 0; agaw--) {
  342. if (test_bit(agaw, &sagaw))
  343. break;
  344. }
  345. return agaw;
  346. }
  347. /* in native case, each domain is related to only one iommu */
  348. static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
  349. {
  350. int iommu_id;
  351. BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
  352. iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
  353. if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
  354. return NULL;
  355. return g_iommus[iommu_id];
  356. }
  357. static void domain_update_iommu_coherency(struct dmar_domain *domain)
  358. {
  359. int i;
  360. domain->iommu_coherency = 1;
  361. i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
  362. for (; i < g_num_of_iommus; ) {
  363. if (!ecap_coherent(g_iommus[i]->ecap)) {
  364. domain->iommu_coherency = 0;
  365. break;
  366. }
  367. i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
  368. }
  369. }
  370. static void domain_update_iommu_snooping(struct dmar_domain *domain)
  371. {
  372. int i;
  373. domain->iommu_snooping = 1;
  374. i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
  375. for (; i < g_num_of_iommus; ) {
  376. if (!ecap_sc_support(g_iommus[i]->ecap)) {
  377. domain->iommu_snooping = 0;
  378. break;
  379. }
  380. i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
  381. }
  382. }
  383. /* Some capabilities may be different across iommus */
  384. static void domain_update_iommu_cap(struct dmar_domain *domain)
  385. {
  386. domain_update_iommu_coherency(domain);
  387. domain_update_iommu_snooping(domain);
  388. }
  389. static struct intel_iommu *device_to_iommu(u8 bus, u8 devfn)
  390. {
  391. struct dmar_drhd_unit *drhd = NULL;
  392. int i;
  393. for_each_drhd_unit(drhd) {
  394. if (drhd->ignored)
  395. continue;
  396. for (i = 0; i < drhd->devices_cnt; i++)
  397. if (drhd->devices[i] &&
  398. drhd->devices[i]->bus->number == bus &&
  399. drhd->devices[i]->devfn == devfn)
  400. return drhd->iommu;
  401. if (drhd->include_all)
  402. return drhd->iommu;
  403. }
  404. return NULL;
  405. }
  406. static void domain_flush_cache(struct dmar_domain *domain,
  407. void *addr, int size)
  408. {
  409. if (!domain->iommu_coherency)
  410. clflush_cache_range(addr, size);
  411. }
  412. /* Gets context entry for a given bus and devfn */
  413. static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
  414. u8 bus, u8 devfn)
  415. {
  416. struct root_entry *root;
  417. struct context_entry *context;
  418. unsigned long phy_addr;
  419. unsigned long flags;
  420. spin_lock_irqsave(&iommu->lock, flags);
  421. root = &iommu->root_entry[bus];
  422. context = get_context_addr_from_root(root);
  423. if (!context) {
  424. context = (struct context_entry *)alloc_pgtable_page();
  425. if (!context) {
  426. spin_unlock_irqrestore(&iommu->lock, flags);
  427. return NULL;
  428. }
  429. __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
  430. phy_addr = virt_to_phys((void *)context);
  431. set_root_value(root, phy_addr);
  432. set_root_present(root);
  433. __iommu_flush_cache(iommu, root, sizeof(*root));
  434. }
  435. spin_unlock_irqrestore(&iommu->lock, flags);
  436. return &context[devfn];
  437. }
  438. static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
  439. {
  440. struct root_entry *root;
  441. struct context_entry *context;
  442. int ret;
  443. unsigned long flags;
  444. spin_lock_irqsave(&iommu->lock, flags);
  445. root = &iommu->root_entry[bus];
  446. context = get_context_addr_from_root(root);
  447. if (!context) {
  448. ret = 0;
  449. goto out;
  450. }
  451. ret = context_present(&context[devfn]);
  452. out:
  453. spin_unlock_irqrestore(&iommu->lock, flags);
  454. return ret;
  455. }
  456. static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
  457. {
  458. struct root_entry *root;
  459. struct context_entry *context;
  460. unsigned long flags;
  461. spin_lock_irqsave(&iommu->lock, flags);
  462. root = &iommu->root_entry[bus];
  463. context = get_context_addr_from_root(root);
  464. if (context) {
  465. context_clear_entry(&context[devfn]);
  466. __iommu_flush_cache(iommu, &context[devfn], \
  467. sizeof(*context));
  468. }
  469. spin_unlock_irqrestore(&iommu->lock, flags);
  470. }
  471. static void free_context_table(struct intel_iommu *iommu)
  472. {
  473. struct root_entry *root;
  474. int i;
  475. unsigned long flags;
  476. struct context_entry *context;
  477. spin_lock_irqsave(&iommu->lock, flags);
  478. if (!iommu->root_entry) {
  479. goto out;
  480. }
  481. for (i = 0; i < ROOT_ENTRY_NR; i++) {
  482. root = &iommu->root_entry[i];
  483. context = get_context_addr_from_root(root);
  484. if (context)
  485. free_pgtable_page(context);
  486. }
  487. free_pgtable_page(iommu->root_entry);
  488. iommu->root_entry = NULL;
  489. out:
  490. spin_unlock_irqrestore(&iommu->lock, flags);
  491. }
  492. /* page table handling */
  493. #define LEVEL_STRIDE (9)
  494. #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
  495. static inline int agaw_to_level(int agaw)
  496. {
  497. return agaw + 2;
  498. }
  499. static inline int agaw_to_width(int agaw)
  500. {
  501. return 30 + agaw * LEVEL_STRIDE;
  502. }
  503. static inline int width_to_agaw(int width)
  504. {
  505. return (width - 30) / LEVEL_STRIDE;
  506. }
  507. static inline unsigned int level_to_offset_bits(int level)
  508. {
  509. return (12 + (level - 1) * LEVEL_STRIDE);
  510. }
  511. static inline int address_level_offset(u64 addr, int level)
  512. {
  513. return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
  514. }
  515. static inline u64 level_mask(int level)
  516. {
  517. return ((u64)-1 << level_to_offset_bits(level));
  518. }
  519. static inline u64 level_size(int level)
  520. {
  521. return ((u64)1 << level_to_offset_bits(level));
  522. }
  523. static inline u64 align_to_level(u64 addr, int level)
  524. {
  525. return ((addr + level_size(level) - 1) & level_mask(level));
  526. }
  527. static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
  528. {
  529. int addr_width = agaw_to_width(domain->agaw);
  530. struct dma_pte *parent, *pte = NULL;
  531. int level = agaw_to_level(domain->agaw);
  532. int offset;
  533. unsigned long flags;
  534. BUG_ON(!domain->pgd);
  535. addr &= (((u64)1) << addr_width) - 1;
  536. parent = domain->pgd;
  537. spin_lock_irqsave(&domain->mapping_lock, flags);
  538. while (level > 0) {
  539. void *tmp_page;
  540. offset = address_level_offset(addr, level);
  541. pte = &parent[offset];
  542. if (level == 1)
  543. break;
  544. if (!dma_pte_present(pte)) {
  545. tmp_page = alloc_pgtable_page();
  546. if (!tmp_page) {
  547. spin_unlock_irqrestore(&domain->mapping_lock,
  548. flags);
  549. return NULL;
  550. }
  551. domain_flush_cache(domain, tmp_page, PAGE_SIZE);
  552. dma_set_pte_addr(pte, virt_to_phys(tmp_page));
  553. /*
  554. * high level table always sets r/w, last level page
  555. * table control read/write
  556. */
  557. dma_set_pte_readable(pte);
  558. dma_set_pte_writable(pte);
  559. domain_flush_cache(domain, pte, sizeof(*pte));
  560. }
  561. parent = phys_to_virt(dma_pte_addr(pte));
  562. level--;
  563. }
  564. spin_unlock_irqrestore(&domain->mapping_lock, flags);
  565. return pte;
  566. }
  567. /* return address's pte at specific level */
  568. static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
  569. int level)
  570. {
  571. struct dma_pte *parent, *pte = NULL;
  572. int total = agaw_to_level(domain->agaw);
  573. int offset;
  574. parent = domain->pgd;
  575. while (level <= total) {
  576. offset = address_level_offset(addr, total);
  577. pte = &parent[offset];
  578. if (level == total)
  579. return pte;
  580. if (!dma_pte_present(pte))
  581. break;
  582. parent = phys_to_virt(dma_pte_addr(pte));
  583. total--;
  584. }
  585. return NULL;
  586. }
  587. /* clear one page's page table */
  588. static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
  589. {
  590. struct dma_pte *pte = NULL;
  591. /* get last level pte */
  592. pte = dma_addr_level_pte(domain, addr, 1);
  593. if (pte) {
  594. dma_clear_pte(pte);
  595. domain_flush_cache(domain, pte, sizeof(*pte));
  596. }
  597. }
  598. /* clear last level pte, a tlb flush should be followed */
  599. static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
  600. {
  601. int addr_width = agaw_to_width(domain->agaw);
  602. int npages;
  603. start &= (((u64)1) << addr_width) - 1;
  604. end &= (((u64)1) << addr_width) - 1;
  605. /* in case it's partial page */
  606. start = PAGE_ALIGN(start);
  607. end &= PAGE_MASK;
  608. npages = (end - start) / VTD_PAGE_SIZE;
  609. /* we don't need lock here, nobody else touches the iova range */
  610. while (npages--) {
  611. dma_pte_clear_one(domain, start);
  612. start += VTD_PAGE_SIZE;
  613. }
  614. }
  615. /* free page table pages. last level pte should already be cleared */
  616. static void dma_pte_free_pagetable(struct dmar_domain *domain,
  617. u64 start, u64 end)
  618. {
  619. int addr_width = agaw_to_width(domain->agaw);
  620. struct dma_pte *pte;
  621. int total = agaw_to_level(domain->agaw);
  622. int level;
  623. u64 tmp;
  624. start &= (((u64)1) << addr_width) - 1;
  625. end &= (((u64)1) << addr_width) - 1;
  626. /* we don't need lock here, nobody else touches the iova range */
  627. level = 2;
  628. while (level <= total) {
  629. tmp = align_to_level(start, level);
  630. if (tmp >= end || (tmp + level_size(level) > end))
  631. return;
  632. while (tmp < end) {
  633. pte = dma_addr_level_pte(domain, tmp, level);
  634. if (pte) {
  635. free_pgtable_page(
  636. phys_to_virt(dma_pte_addr(pte)));
  637. dma_clear_pte(pte);
  638. domain_flush_cache(domain, pte, sizeof(*pte));
  639. }
  640. tmp += level_size(level);
  641. }
  642. level++;
  643. }
  644. /* free pgd */
  645. if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
  646. free_pgtable_page(domain->pgd);
  647. domain->pgd = NULL;
  648. }
  649. }
  650. /* iommu handling */
  651. static int iommu_alloc_root_entry(struct intel_iommu *iommu)
  652. {
  653. struct root_entry *root;
  654. unsigned long flags;
  655. root = (struct root_entry *)alloc_pgtable_page();
  656. if (!root)
  657. return -ENOMEM;
  658. __iommu_flush_cache(iommu, root, ROOT_SIZE);
  659. spin_lock_irqsave(&iommu->lock, flags);
  660. iommu->root_entry = root;
  661. spin_unlock_irqrestore(&iommu->lock, flags);
  662. return 0;
  663. }
  664. static void iommu_set_root_entry(struct intel_iommu *iommu)
  665. {
  666. void *addr;
  667. u32 cmd, sts;
  668. unsigned long flag;
  669. addr = iommu->root_entry;
  670. spin_lock_irqsave(&iommu->register_lock, flag);
  671. dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
  672. cmd = iommu->gcmd | DMA_GCMD_SRTP;
  673. writel(cmd, iommu->reg + DMAR_GCMD_REG);
  674. /* Make sure hardware complete it */
  675. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  676. readl, (sts & DMA_GSTS_RTPS), sts);
  677. spin_unlock_irqrestore(&iommu->register_lock, flag);
  678. }
  679. static void iommu_flush_write_buffer(struct intel_iommu *iommu)
  680. {
  681. u32 val;
  682. unsigned long flag;
  683. if (!rwbf_quirk && !cap_rwbf(iommu->cap))
  684. return;
  685. val = iommu->gcmd | DMA_GCMD_WBF;
  686. spin_lock_irqsave(&iommu->register_lock, flag);
  687. writel(val, iommu->reg + DMAR_GCMD_REG);
  688. /* Make sure hardware complete it */
  689. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  690. readl, (!(val & DMA_GSTS_WBFS)), val);
  691. spin_unlock_irqrestore(&iommu->register_lock, flag);
  692. }
  693. /* return value determine if we need a write buffer flush */
  694. static int __iommu_flush_context(struct intel_iommu *iommu,
  695. u16 did, u16 source_id, u8 function_mask, u64 type,
  696. int non_present_entry_flush)
  697. {
  698. u64 val = 0;
  699. unsigned long flag;
  700. /*
  701. * In the non-present entry flush case, if hardware doesn't cache
  702. * non-present entry we do nothing and if hardware cache non-present
  703. * entry, we flush entries of domain 0 (the domain id is used to cache
  704. * any non-present entries)
  705. */
  706. if (non_present_entry_flush) {
  707. if (!cap_caching_mode(iommu->cap))
  708. return 1;
  709. else
  710. did = 0;
  711. }
  712. switch (type) {
  713. case DMA_CCMD_GLOBAL_INVL:
  714. val = DMA_CCMD_GLOBAL_INVL;
  715. break;
  716. case DMA_CCMD_DOMAIN_INVL:
  717. val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
  718. break;
  719. case DMA_CCMD_DEVICE_INVL:
  720. val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
  721. | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
  722. break;
  723. default:
  724. BUG();
  725. }
  726. val |= DMA_CCMD_ICC;
  727. spin_lock_irqsave(&iommu->register_lock, flag);
  728. dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
  729. /* Make sure hardware complete it */
  730. IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
  731. dmar_readq, (!(val & DMA_CCMD_ICC)), val);
  732. spin_unlock_irqrestore(&iommu->register_lock, flag);
  733. /* flush context entry will implicitly flush write buffer */
  734. return 0;
  735. }
  736. /* return value determine if we need a write buffer flush */
  737. static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
  738. u64 addr, unsigned int size_order, u64 type,
  739. int non_present_entry_flush)
  740. {
  741. int tlb_offset = ecap_iotlb_offset(iommu->ecap);
  742. u64 val = 0, val_iva = 0;
  743. unsigned long flag;
  744. /*
  745. * In the non-present entry flush case, if hardware doesn't cache
  746. * non-present entry we do nothing and if hardware cache non-present
  747. * entry, we flush entries of domain 0 (the domain id is used to cache
  748. * any non-present entries)
  749. */
  750. if (non_present_entry_flush) {
  751. if (!cap_caching_mode(iommu->cap))
  752. return 1;
  753. else
  754. did = 0;
  755. }
  756. switch (type) {
  757. case DMA_TLB_GLOBAL_FLUSH:
  758. /* global flush doesn't need set IVA_REG */
  759. val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
  760. break;
  761. case DMA_TLB_DSI_FLUSH:
  762. val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
  763. break;
  764. case DMA_TLB_PSI_FLUSH:
  765. val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
  766. /* Note: always flush non-leaf currently */
  767. val_iva = size_order | addr;
  768. break;
  769. default:
  770. BUG();
  771. }
  772. /* Note: set drain read/write */
  773. #if 0
  774. /*
  775. * This is probably to be super secure.. Looks like we can
  776. * ignore it without any impact.
  777. */
  778. if (cap_read_drain(iommu->cap))
  779. val |= DMA_TLB_READ_DRAIN;
  780. #endif
  781. if (cap_write_drain(iommu->cap))
  782. val |= DMA_TLB_WRITE_DRAIN;
  783. spin_lock_irqsave(&iommu->register_lock, flag);
  784. /* Note: Only uses first TLB reg currently */
  785. if (val_iva)
  786. dmar_writeq(iommu->reg + tlb_offset, val_iva);
  787. dmar_writeq(iommu->reg + tlb_offset + 8, val);
  788. /* Make sure hardware complete it */
  789. IOMMU_WAIT_OP(iommu, tlb_offset + 8,
  790. dmar_readq, (!(val & DMA_TLB_IVT)), val);
  791. spin_unlock_irqrestore(&iommu->register_lock, flag);
  792. /* check IOTLB invalidation granularity */
  793. if (DMA_TLB_IAIG(val) == 0)
  794. printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
  795. if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
  796. pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
  797. (unsigned long long)DMA_TLB_IIRG(type),
  798. (unsigned long long)DMA_TLB_IAIG(val));
  799. /* flush iotlb entry will implicitly flush write buffer */
  800. return 0;
  801. }
  802. static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
  803. u64 addr, unsigned int pages, int non_present_entry_flush)
  804. {
  805. unsigned int mask;
  806. BUG_ON(addr & (~VTD_PAGE_MASK));
  807. BUG_ON(pages == 0);
  808. /* Fallback to domain selective flush if no PSI support */
  809. if (!cap_pgsel_inv(iommu->cap))
  810. return iommu->flush.flush_iotlb(iommu, did, 0, 0,
  811. DMA_TLB_DSI_FLUSH,
  812. non_present_entry_flush);
  813. /*
  814. * PSI requires page size to be 2 ^ x, and the base address is naturally
  815. * aligned to the size
  816. */
  817. mask = ilog2(__roundup_pow_of_two(pages));
  818. /* Fallback to domain selective flush if size is too big */
  819. if (mask > cap_max_amask_val(iommu->cap))
  820. return iommu->flush.flush_iotlb(iommu, did, 0, 0,
  821. DMA_TLB_DSI_FLUSH, non_present_entry_flush);
  822. return iommu->flush.flush_iotlb(iommu, did, addr, mask,
  823. DMA_TLB_PSI_FLUSH,
  824. non_present_entry_flush);
  825. }
  826. static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
  827. {
  828. u32 pmen;
  829. unsigned long flags;
  830. spin_lock_irqsave(&iommu->register_lock, flags);
  831. pmen = readl(iommu->reg + DMAR_PMEN_REG);
  832. pmen &= ~DMA_PMEN_EPM;
  833. writel(pmen, iommu->reg + DMAR_PMEN_REG);
  834. /* wait for the protected region status bit to clear */
  835. IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
  836. readl, !(pmen & DMA_PMEN_PRS), pmen);
  837. spin_unlock_irqrestore(&iommu->register_lock, flags);
  838. }
  839. static int iommu_enable_translation(struct intel_iommu *iommu)
  840. {
  841. u32 sts;
  842. unsigned long flags;
  843. spin_lock_irqsave(&iommu->register_lock, flags);
  844. writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
  845. /* Make sure hardware complete it */
  846. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  847. readl, (sts & DMA_GSTS_TES), sts);
  848. iommu->gcmd |= DMA_GCMD_TE;
  849. spin_unlock_irqrestore(&iommu->register_lock, flags);
  850. return 0;
  851. }
  852. static int iommu_disable_translation(struct intel_iommu *iommu)
  853. {
  854. u32 sts;
  855. unsigned long flag;
  856. spin_lock_irqsave(&iommu->register_lock, flag);
  857. iommu->gcmd &= ~DMA_GCMD_TE;
  858. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  859. /* Make sure hardware complete it */
  860. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  861. readl, (!(sts & DMA_GSTS_TES)), sts);
  862. spin_unlock_irqrestore(&iommu->register_lock, flag);
  863. return 0;
  864. }
  865. static int iommu_init_domains(struct intel_iommu *iommu)
  866. {
  867. unsigned long ndomains;
  868. unsigned long nlongs;
  869. ndomains = cap_ndoms(iommu->cap);
  870. pr_debug("Number of Domains supportd <%ld>\n", ndomains);
  871. nlongs = BITS_TO_LONGS(ndomains);
  872. /* TBD: there might be 64K domains,
  873. * consider other allocation for future chip
  874. */
  875. iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
  876. if (!iommu->domain_ids) {
  877. printk(KERN_ERR "Allocating domain id array failed\n");
  878. return -ENOMEM;
  879. }
  880. iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
  881. GFP_KERNEL);
  882. if (!iommu->domains) {
  883. printk(KERN_ERR "Allocating domain array failed\n");
  884. kfree(iommu->domain_ids);
  885. return -ENOMEM;
  886. }
  887. spin_lock_init(&iommu->lock);
  888. /*
  889. * if Caching mode is set, then invalid translations are tagged
  890. * with domainid 0. Hence we need to pre-allocate it.
  891. */
  892. if (cap_caching_mode(iommu->cap))
  893. set_bit(0, iommu->domain_ids);
  894. return 0;
  895. }
  896. static void domain_exit(struct dmar_domain *domain);
  897. static void vm_domain_exit(struct dmar_domain *domain);
  898. void free_dmar_iommu(struct intel_iommu *iommu)
  899. {
  900. struct dmar_domain *domain;
  901. int i;
  902. unsigned long flags;
  903. i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
  904. for (; i < cap_ndoms(iommu->cap); ) {
  905. domain = iommu->domains[i];
  906. clear_bit(i, iommu->domain_ids);
  907. spin_lock_irqsave(&domain->iommu_lock, flags);
  908. if (--domain->iommu_count == 0) {
  909. if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
  910. vm_domain_exit(domain);
  911. else
  912. domain_exit(domain);
  913. }
  914. spin_unlock_irqrestore(&domain->iommu_lock, flags);
  915. i = find_next_bit(iommu->domain_ids,
  916. cap_ndoms(iommu->cap), i+1);
  917. }
  918. if (iommu->gcmd & DMA_GCMD_TE)
  919. iommu_disable_translation(iommu);
  920. if (iommu->irq) {
  921. set_irq_data(iommu->irq, NULL);
  922. /* This will mask the irq */
  923. free_irq(iommu->irq, iommu);
  924. destroy_irq(iommu->irq);
  925. }
  926. kfree(iommu->domains);
  927. kfree(iommu->domain_ids);
  928. g_iommus[iommu->seq_id] = NULL;
  929. /* if all iommus are freed, free g_iommus */
  930. for (i = 0; i < g_num_of_iommus; i++) {
  931. if (g_iommus[i])
  932. break;
  933. }
  934. if (i == g_num_of_iommus)
  935. kfree(g_iommus);
  936. /* free context mapping */
  937. free_context_table(iommu);
  938. }
  939. static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
  940. {
  941. unsigned long num;
  942. unsigned long ndomains;
  943. struct dmar_domain *domain;
  944. unsigned long flags;
  945. domain = alloc_domain_mem();
  946. if (!domain)
  947. return NULL;
  948. ndomains = cap_ndoms(iommu->cap);
  949. spin_lock_irqsave(&iommu->lock, flags);
  950. num = find_first_zero_bit(iommu->domain_ids, ndomains);
  951. if (num >= ndomains) {
  952. spin_unlock_irqrestore(&iommu->lock, flags);
  953. free_domain_mem(domain);
  954. printk(KERN_ERR "IOMMU: no free domain ids\n");
  955. return NULL;
  956. }
  957. set_bit(num, iommu->domain_ids);
  958. domain->id = num;
  959. memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
  960. set_bit(iommu->seq_id, &domain->iommu_bmp);
  961. domain->flags = 0;
  962. iommu->domains[num] = domain;
  963. spin_unlock_irqrestore(&iommu->lock, flags);
  964. return domain;
  965. }
  966. static void iommu_free_domain(struct dmar_domain *domain)
  967. {
  968. unsigned long flags;
  969. struct intel_iommu *iommu;
  970. iommu = domain_get_iommu(domain);
  971. spin_lock_irqsave(&iommu->lock, flags);
  972. clear_bit(domain->id, iommu->domain_ids);
  973. spin_unlock_irqrestore(&iommu->lock, flags);
  974. }
  975. static struct iova_domain reserved_iova_list;
  976. static struct lock_class_key reserved_alloc_key;
  977. static struct lock_class_key reserved_rbtree_key;
  978. static void dmar_init_reserved_ranges(void)
  979. {
  980. struct pci_dev *pdev = NULL;
  981. struct iova *iova;
  982. int i;
  983. u64 addr, size;
  984. init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
  985. lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
  986. &reserved_alloc_key);
  987. lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
  988. &reserved_rbtree_key);
  989. /* IOAPIC ranges shouldn't be accessed by DMA */
  990. iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
  991. IOVA_PFN(IOAPIC_RANGE_END));
  992. if (!iova)
  993. printk(KERN_ERR "Reserve IOAPIC range failed\n");
  994. /* Reserve all PCI MMIO to avoid peer-to-peer access */
  995. for_each_pci_dev(pdev) {
  996. struct resource *r;
  997. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  998. r = &pdev->resource[i];
  999. if (!r->flags || !(r->flags & IORESOURCE_MEM))
  1000. continue;
  1001. addr = r->start;
  1002. addr &= PAGE_MASK;
  1003. size = r->end - addr;
  1004. size = PAGE_ALIGN(size);
  1005. iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
  1006. IOVA_PFN(size + addr) - 1);
  1007. if (!iova)
  1008. printk(KERN_ERR "Reserve iova failed\n");
  1009. }
  1010. }
  1011. }
  1012. static void domain_reserve_special_ranges(struct dmar_domain *domain)
  1013. {
  1014. copy_reserved_iova(&reserved_iova_list, &domain->iovad);
  1015. }
  1016. static inline int guestwidth_to_adjustwidth(int gaw)
  1017. {
  1018. int agaw;
  1019. int r = (gaw - 12) % 9;
  1020. if (r == 0)
  1021. agaw = gaw;
  1022. else
  1023. agaw = gaw + 9 - r;
  1024. if (agaw > 64)
  1025. agaw = 64;
  1026. return agaw;
  1027. }
  1028. static int domain_init(struct dmar_domain *domain, int guest_width)
  1029. {
  1030. struct intel_iommu *iommu;
  1031. int adjust_width, agaw;
  1032. unsigned long sagaw;
  1033. init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
  1034. spin_lock_init(&domain->mapping_lock);
  1035. spin_lock_init(&domain->iommu_lock);
  1036. domain_reserve_special_ranges(domain);
  1037. /* calculate AGAW */
  1038. iommu = domain_get_iommu(domain);
  1039. if (guest_width > cap_mgaw(iommu->cap))
  1040. guest_width = cap_mgaw(iommu->cap);
  1041. domain->gaw = guest_width;
  1042. adjust_width = guestwidth_to_adjustwidth(guest_width);
  1043. agaw = width_to_agaw(adjust_width);
  1044. sagaw = cap_sagaw(iommu->cap);
  1045. if (!test_bit(agaw, &sagaw)) {
  1046. /* hardware doesn't support it, choose a bigger one */
  1047. pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
  1048. agaw = find_next_bit(&sagaw, 5, agaw);
  1049. if (agaw >= 5)
  1050. return -ENODEV;
  1051. }
  1052. domain->agaw = agaw;
  1053. INIT_LIST_HEAD(&domain->devices);
  1054. if (ecap_coherent(iommu->ecap))
  1055. domain->iommu_coherency = 1;
  1056. else
  1057. domain->iommu_coherency = 0;
  1058. if (ecap_sc_support(iommu->ecap))
  1059. domain->iommu_snooping = 1;
  1060. else
  1061. domain->iommu_snooping = 0;
  1062. domain->iommu_count = 1;
  1063. /* always allocate the top pgd */
  1064. domain->pgd = (struct dma_pte *)alloc_pgtable_page();
  1065. if (!domain->pgd)
  1066. return -ENOMEM;
  1067. __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
  1068. return 0;
  1069. }
  1070. static void domain_exit(struct dmar_domain *domain)
  1071. {
  1072. u64 end;
  1073. /* Domain 0 is reserved, so dont process it */
  1074. if (!domain)
  1075. return;
  1076. domain_remove_dev_info(domain);
  1077. /* destroy iovas */
  1078. put_iova_domain(&domain->iovad);
  1079. end = DOMAIN_MAX_ADDR(domain->gaw);
  1080. end = end & (~PAGE_MASK);
  1081. /* clear ptes */
  1082. dma_pte_clear_range(domain, 0, end);
  1083. /* free page tables */
  1084. dma_pte_free_pagetable(domain, 0, end);
  1085. iommu_free_domain(domain);
  1086. free_domain_mem(domain);
  1087. }
  1088. static int domain_context_mapping_one(struct dmar_domain *domain,
  1089. u8 bus, u8 devfn)
  1090. {
  1091. struct context_entry *context;
  1092. unsigned long flags;
  1093. struct intel_iommu *iommu;
  1094. struct dma_pte *pgd;
  1095. unsigned long num;
  1096. unsigned long ndomains;
  1097. int id;
  1098. int agaw;
  1099. pr_debug("Set context mapping for %02x:%02x.%d\n",
  1100. bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  1101. BUG_ON(!domain->pgd);
  1102. iommu = device_to_iommu(bus, devfn);
  1103. if (!iommu)
  1104. return -ENODEV;
  1105. context = device_to_context_entry(iommu, bus, devfn);
  1106. if (!context)
  1107. return -ENOMEM;
  1108. spin_lock_irqsave(&iommu->lock, flags);
  1109. if (context_present(context)) {
  1110. spin_unlock_irqrestore(&iommu->lock, flags);
  1111. return 0;
  1112. }
  1113. id = domain->id;
  1114. pgd = domain->pgd;
  1115. if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) {
  1116. int found = 0;
  1117. /* find an available domain id for this device in iommu */
  1118. ndomains = cap_ndoms(iommu->cap);
  1119. num = find_first_bit(iommu->domain_ids, ndomains);
  1120. for (; num < ndomains; ) {
  1121. if (iommu->domains[num] == domain) {
  1122. id = num;
  1123. found = 1;
  1124. break;
  1125. }
  1126. num = find_next_bit(iommu->domain_ids,
  1127. cap_ndoms(iommu->cap), num+1);
  1128. }
  1129. if (found == 0) {
  1130. num = find_first_zero_bit(iommu->domain_ids, ndomains);
  1131. if (num >= ndomains) {
  1132. spin_unlock_irqrestore(&iommu->lock, flags);
  1133. printk(KERN_ERR "IOMMU: no free domain ids\n");
  1134. return -EFAULT;
  1135. }
  1136. set_bit(num, iommu->domain_ids);
  1137. iommu->domains[num] = domain;
  1138. id = num;
  1139. }
  1140. /* Skip top levels of page tables for
  1141. * iommu which has less agaw than default.
  1142. */
  1143. for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
  1144. pgd = phys_to_virt(dma_pte_addr(pgd));
  1145. if (!dma_pte_present(pgd)) {
  1146. spin_unlock_irqrestore(&iommu->lock, flags);
  1147. return -ENOMEM;
  1148. }
  1149. }
  1150. }
  1151. context_set_domain_id(context, id);
  1152. context_set_address_width(context, iommu->agaw);
  1153. context_set_address_root(context, virt_to_phys(pgd));
  1154. context_set_translation_type(context, CONTEXT_TT_MULTI_LEVEL);
  1155. context_set_fault_enable(context);
  1156. context_set_present(context);
  1157. domain_flush_cache(domain, context, sizeof(*context));
  1158. /* it's a non-present to present mapping */
  1159. if (iommu->flush.flush_context(iommu, domain->id,
  1160. (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
  1161. DMA_CCMD_DEVICE_INVL, 1))
  1162. iommu_flush_write_buffer(iommu);
  1163. else
  1164. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
  1165. spin_unlock_irqrestore(&iommu->lock, flags);
  1166. spin_lock_irqsave(&domain->iommu_lock, flags);
  1167. if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
  1168. domain->iommu_count++;
  1169. domain_update_iommu_cap(domain);
  1170. }
  1171. spin_unlock_irqrestore(&domain->iommu_lock, flags);
  1172. return 0;
  1173. }
  1174. static int
  1175. domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
  1176. {
  1177. int ret;
  1178. struct pci_dev *tmp, *parent;
  1179. ret = domain_context_mapping_one(domain, pdev->bus->number,
  1180. pdev->devfn);
  1181. if (ret)
  1182. return ret;
  1183. /* dependent device mapping */
  1184. tmp = pci_find_upstream_pcie_bridge(pdev);
  1185. if (!tmp)
  1186. return 0;
  1187. /* Secondary interface's bus number and devfn 0 */
  1188. parent = pdev->bus->self;
  1189. while (parent != tmp) {
  1190. ret = domain_context_mapping_one(domain, parent->bus->number,
  1191. parent->devfn);
  1192. if (ret)
  1193. return ret;
  1194. parent = parent->bus->self;
  1195. }
  1196. if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
  1197. return domain_context_mapping_one(domain,
  1198. tmp->subordinate->number, 0);
  1199. else /* this is a legacy PCI bridge */
  1200. return domain_context_mapping_one(domain,
  1201. tmp->bus->number, tmp->devfn);
  1202. }
  1203. static int domain_context_mapped(struct pci_dev *pdev)
  1204. {
  1205. int ret;
  1206. struct pci_dev *tmp, *parent;
  1207. struct intel_iommu *iommu;
  1208. iommu = device_to_iommu(pdev->bus->number, pdev->devfn);
  1209. if (!iommu)
  1210. return -ENODEV;
  1211. ret = device_context_mapped(iommu,
  1212. pdev->bus->number, pdev->devfn);
  1213. if (!ret)
  1214. return ret;
  1215. /* dependent device mapping */
  1216. tmp = pci_find_upstream_pcie_bridge(pdev);
  1217. if (!tmp)
  1218. return ret;
  1219. /* Secondary interface's bus number and devfn 0 */
  1220. parent = pdev->bus->self;
  1221. while (parent != tmp) {
  1222. ret = device_context_mapped(iommu, parent->bus->number,
  1223. parent->devfn);
  1224. if (!ret)
  1225. return ret;
  1226. parent = parent->bus->self;
  1227. }
  1228. if (tmp->is_pcie)
  1229. return device_context_mapped(iommu,
  1230. tmp->subordinate->number, 0);
  1231. else
  1232. return device_context_mapped(iommu,
  1233. tmp->bus->number, tmp->devfn);
  1234. }
  1235. static int
  1236. domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
  1237. u64 hpa, size_t size, int prot)
  1238. {
  1239. u64 start_pfn, end_pfn;
  1240. struct dma_pte *pte;
  1241. int index;
  1242. int addr_width = agaw_to_width(domain->agaw);
  1243. hpa &= (((u64)1) << addr_width) - 1;
  1244. if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
  1245. return -EINVAL;
  1246. iova &= PAGE_MASK;
  1247. start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
  1248. end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
  1249. index = 0;
  1250. while (start_pfn < end_pfn) {
  1251. pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
  1252. if (!pte)
  1253. return -ENOMEM;
  1254. /* We don't need lock here, nobody else
  1255. * touches the iova range
  1256. */
  1257. BUG_ON(dma_pte_addr(pte));
  1258. dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
  1259. dma_set_pte_prot(pte, prot);
  1260. if (prot & DMA_PTE_SNP)
  1261. dma_set_pte_snp(pte);
  1262. domain_flush_cache(domain, pte, sizeof(*pte));
  1263. start_pfn++;
  1264. index++;
  1265. }
  1266. return 0;
  1267. }
  1268. static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
  1269. {
  1270. if (!iommu)
  1271. return;
  1272. clear_context_table(iommu, bus, devfn);
  1273. iommu->flush.flush_context(iommu, 0, 0, 0,
  1274. DMA_CCMD_GLOBAL_INVL, 0);
  1275. iommu->flush.flush_iotlb(iommu, 0, 0, 0,
  1276. DMA_TLB_GLOBAL_FLUSH, 0);
  1277. }
  1278. static void domain_remove_dev_info(struct dmar_domain *domain)
  1279. {
  1280. struct device_domain_info *info;
  1281. unsigned long flags;
  1282. struct intel_iommu *iommu;
  1283. spin_lock_irqsave(&device_domain_lock, flags);
  1284. while (!list_empty(&domain->devices)) {
  1285. info = list_entry(domain->devices.next,
  1286. struct device_domain_info, link);
  1287. list_del(&info->link);
  1288. list_del(&info->global);
  1289. if (info->dev)
  1290. info->dev->dev.archdata.iommu = NULL;
  1291. spin_unlock_irqrestore(&device_domain_lock, flags);
  1292. iommu = device_to_iommu(info->bus, info->devfn);
  1293. iommu_detach_dev(iommu, info->bus, info->devfn);
  1294. free_devinfo_mem(info);
  1295. spin_lock_irqsave(&device_domain_lock, flags);
  1296. }
  1297. spin_unlock_irqrestore(&device_domain_lock, flags);
  1298. }
  1299. /*
  1300. * find_domain
  1301. * Note: we use struct pci_dev->dev.archdata.iommu stores the info
  1302. */
  1303. static struct dmar_domain *
  1304. find_domain(struct pci_dev *pdev)
  1305. {
  1306. struct device_domain_info *info;
  1307. /* No lock here, assumes no domain exit in normal case */
  1308. info = pdev->dev.archdata.iommu;
  1309. if (info)
  1310. return info->domain;
  1311. return NULL;
  1312. }
  1313. /* domain is initialized */
  1314. static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
  1315. {
  1316. struct dmar_domain *domain, *found = NULL;
  1317. struct intel_iommu *iommu;
  1318. struct dmar_drhd_unit *drhd;
  1319. struct device_domain_info *info, *tmp;
  1320. struct pci_dev *dev_tmp;
  1321. unsigned long flags;
  1322. int bus = 0, devfn = 0;
  1323. domain = find_domain(pdev);
  1324. if (domain)
  1325. return domain;
  1326. dev_tmp = pci_find_upstream_pcie_bridge(pdev);
  1327. if (dev_tmp) {
  1328. if (dev_tmp->is_pcie) {
  1329. bus = dev_tmp->subordinate->number;
  1330. devfn = 0;
  1331. } else {
  1332. bus = dev_tmp->bus->number;
  1333. devfn = dev_tmp->devfn;
  1334. }
  1335. spin_lock_irqsave(&device_domain_lock, flags);
  1336. list_for_each_entry(info, &device_domain_list, global) {
  1337. if (info->bus == bus && info->devfn == devfn) {
  1338. found = info->domain;
  1339. break;
  1340. }
  1341. }
  1342. spin_unlock_irqrestore(&device_domain_lock, flags);
  1343. /* pcie-pci bridge already has a domain, uses it */
  1344. if (found) {
  1345. domain = found;
  1346. goto found_domain;
  1347. }
  1348. }
  1349. /* Allocate new domain for the device */
  1350. drhd = dmar_find_matched_drhd_unit(pdev);
  1351. if (!drhd) {
  1352. printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
  1353. pci_name(pdev));
  1354. return NULL;
  1355. }
  1356. iommu = drhd->iommu;
  1357. domain = iommu_alloc_domain(iommu);
  1358. if (!domain)
  1359. goto error;
  1360. if (domain_init(domain, gaw)) {
  1361. domain_exit(domain);
  1362. goto error;
  1363. }
  1364. /* register pcie-to-pci device */
  1365. if (dev_tmp) {
  1366. info = alloc_devinfo_mem();
  1367. if (!info) {
  1368. domain_exit(domain);
  1369. goto error;
  1370. }
  1371. info->bus = bus;
  1372. info->devfn = devfn;
  1373. info->dev = NULL;
  1374. info->domain = domain;
  1375. /* This domain is shared by devices under p2p bridge */
  1376. domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
  1377. /* pcie-to-pci bridge already has a domain, uses it */
  1378. found = NULL;
  1379. spin_lock_irqsave(&device_domain_lock, flags);
  1380. list_for_each_entry(tmp, &device_domain_list, global) {
  1381. if (tmp->bus == bus && tmp->devfn == devfn) {
  1382. found = tmp->domain;
  1383. break;
  1384. }
  1385. }
  1386. if (found) {
  1387. free_devinfo_mem(info);
  1388. domain_exit(domain);
  1389. domain = found;
  1390. } else {
  1391. list_add(&info->link, &domain->devices);
  1392. list_add(&info->global, &device_domain_list);
  1393. }
  1394. spin_unlock_irqrestore(&device_domain_lock, flags);
  1395. }
  1396. found_domain:
  1397. info = alloc_devinfo_mem();
  1398. if (!info)
  1399. goto error;
  1400. info->bus = pdev->bus->number;
  1401. info->devfn = pdev->devfn;
  1402. info->dev = pdev;
  1403. info->domain = domain;
  1404. spin_lock_irqsave(&device_domain_lock, flags);
  1405. /* somebody is fast */
  1406. found = find_domain(pdev);
  1407. if (found != NULL) {
  1408. spin_unlock_irqrestore(&device_domain_lock, flags);
  1409. if (found != domain) {
  1410. domain_exit(domain);
  1411. domain = found;
  1412. }
  1413. free_devinfo_mem(info);
  1414. return domain;
  1415. }
  1416. list_add(&info->link, &domain->devices);
  1417. list_add(&info->global, &device_domain_list);
  1418. pdev->dev.archdata.iommu = info;
  1419. spin_unlock_irqrestore(&device_domain_lock, flags);
  1420. return domain;
  1421. error:
  1422. /* recheck it here, maybe others set it */
  1423. return find_domain(pdev);
  1424. }
  1425. static int iommu_prepare_identity_map(struct pci_dev *pdev,
  1426. unsigned long long start,
  1427. unsigned long long end)
  1428. {
  1429. struct dmar_domain *domain;
  1430. unsigned long size;
  1431. unsigned long long base;
  1432. int ret;
  1433. printk(KERN_INFO
  1434. "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
  1435. pci_name(pdev), start, end);
  1436. /* page table init */
  1437. domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
  1438. if (!domain)
  1439. return -ENOMEM;
  1440. /* The address might not be aligned */
  1441. base = start & PAGE_MASK;
  1442. size = end - base;
  1443. size = PAGE_ALIGN(size);
  1444. if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
  1445. IOVA_PFN(base + size) - 1)) {
  1446. printk(KERN_ERR "IOMMU: reserve iova failed\n");
  1447. ret = -ENOMEM;
  1448. goto error;
  1449. }
  1450. pr_debug("Mapping reserved region %lx@%llx for %s\n",
  1451. size, base, pci_name(pdev));
  1452. /*
  1453. * RMRR range might have overlap with physical memory range,
  1454. * clear it first
  1455. */
  1456. dma_pte_clear_range(domain, base, base + size);
  1457. ret = domain_page_mapping(domain, base, base, size,
  1458. DMA_PTE_READ|DMA_PTE_WRITE);
  1459. if (ret)
  1460. goto error;
  1461. /* context entry init */
  1462. ret = domain_context_mapping(domain, pdev);
  1463. if (!ret)
  1464. return 0;
  1465. error:
  1466. domain_exit(domain);
  1467. return ret;
  1468. }
  1469. static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
  1470. struct pci_dev *pdev)
  1471. {
  1472. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  1473. return 0;
  1474. return iommu_prepare_identity_map(pdev, rmrr->base_address,
  1475. rmrr->end_address + 1);
  1476. }
  1477. #ifdef CONFIG_DMAR_GFX_WA
  1478. struct iommu_prepare_data {
  1479. struct pci_dev *pdev;
  1480. int ret;
  1481. };
  1482. static int __init iommu_prepare_work_fn(unsigned long start_pfn,
  1483. unsigned long end_pfn, void *datax)
  1484. {
  1485. struct iommu_prepare_data *data;
  1486. data = (struct iommu_prepare_data *)datax;
  1487. data->ret = iommu_prepare_identity_map(data->pdev,
  1488. start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
  1489. return data->ret;
  1490. }
  1491. static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
  1492. {
  1493. int nid;
  1494. struct iommu_prepare_data data;
  1495. data.pdev = pdev;
  1496. data.ret = 0;
  1497. for_each_online_node(nid) {
  1498. work_with_active_regions(nid, iommu_prepare_work_fn, &data);
  1499. if (data.ret)
  1500. return data.ret;
  1501. }
  1502. return data.ret;
  1503. }
  1504. static void __init iommu_prepare_gfx_mapping(void)
  1505. {
  1506. struct pci_dev *pdev = NULL;
  1507. int ret;
  1508. for_each_pci_dev(pdev) {
  1509. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
  1510. !IS_GFX_DEVICE(pdev))
  1511. continue;
  1512. printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
  1513. pci_name(pdev));
  1514. ret = iommu_prepare_with_active_regions(pdev);
  1515. if (ret)
  1516. printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
  1517. }
  1518. }
  1519. #else /* !CONFIG_DMAR_GFX_WA */
  1520. static inline void iommu_prepare_gfx_mapping(void)
  1521. {
  1522. return;
  1523. }
  1524. #endif
  1525. #ifdef CONFIG_DMAR_FLOPPY_WA
  1526. static inline void iommu_prepare_isa(void)
  1527. {
  1528. struct pci_dev *pdev;
  1529. int ret;
  1530. pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  1531. if (!pdev)
  1532. return;
  1533. printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
  1534. ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
  1535. if (ret)
  1536. printk(KERN_ERR "IOMMU: Failed to create 0-64M identity map, "
  1537. "floppy might not work\n");
  1538. }
  1539. #else
  1540. static inline void iommu_prepare_isa(void)
  1541. {
  1542. return;
  1543. }
  1544. #endif /* !CONFIG_DMAR_FLPY_WA */
  1545. static int __init init_dmars(void)
  1546. {
  1547. struct dmar_drhd_unit *drhd;
  1548. struct dmar_rmrr_unit *rmrr;
  1549. struct pci_dev *pdev;
  1550. struct intel_iommu *iommu;
  1551. int i, ret;
  1552. /*
  1553. * for each drhd
  1554. * allocate root
  1555. * initialize and program root entry to not present
  1556. * endfor
  1557. */
  1558. for_each_drhd_unit(drhd) {
  1559. g_num_of_iommus++;
  1560. /*
  1561. * lock not needed as this is only incremented in the single
  1562. * threaded kernel __init code path all other access are read
  1563. * only
  1564. */
  1565. }
  1566. g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
  1567. GFP_KERNEL);
  1568. if (!g_iommus) {
  1569. printk(KERN_ERR "Allocating global iommu array failed\n");
  1570. ret = -ENOMEM;
  1571. goto error;
  1572. }
  1573. deferred_flush = kzalloc(g_num_of_iommus *
  1574. sizeof(struct deferred_flush_tables), GFP_KERNEL);
  1575. if (!deferred_flush) {
  1576. kfree(g_iommus);
  1577. ret = -ENOMEM;
  1578. goto error;
  1579. }
  1580. for_each_drhd_unit(drhd) {
  1581. if (drhd->ignored)
  1582. continue;
  1583. iommu = drhd->iommu;
  1584. g_iommus[iommu->seq_id] = iommu;
  1585. ret = iommu_init_domains(iommu);
  1586. if (ret)
  1587. goto error;
  1588. /*
  1589. * TBD:
  1590. * we could share the same root & context tables
  1591. * amoung all IOMMU's. Need to Split it later.
  1592. */
  1593. ret = iommu_alloc_root_entry(iommu);
  1594. if (ret) {
  1595. printk(KERN_ERR "IOMMU: allocate root entry failed\n");
  1596. goto error;
  1597. }
  1598. }
  1599. /*
  1600. * Start from the sane iommu hardware state.
  1601. */
  1602. for_each_drhd_unit(drhd) {
  1603. if (drhd->ignored)
  1604. continue;
  1605. iommu = drhd->iommu;
  1606. /*
  1607. * If the queued invalidation is already initialized by us
  1608. * (for example, while enabling interrupt-remapping) then
  1609. * we got the things already rolling from a sane state.
  1610. */
  1611. if (iommu->qi)
  1612. continue;
  1613. /*
  1614. * Clear any previous faults.
  1615. */
  1616. dmar_fault(-1, iommu);
  1617. /*
  1618. * Disable queued invalidation if supported and already enabled
  1619. * before OS handover.
  1620. */
  1621. dmar_disable_qi(iommu);
  1622. }
  1623. for_each_drhd_unit(drhd) {
  1624. if (drhd->ignored)
  1625. continue;
  1626. iommu = drhd->iommu;
  1627. if (dmar_enable_qi(iommu)) {
  1628. /*
  1629. * Queued Invalidate not enabled, use Register Based
  1630. * Invalidate
  1631. */
  1632. iommu->flush.flush_context = __iommu_flush_context;
  1633. iommu->flush.flush_iotlb = __iommu_flush_iotlb;
  1634. printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
  1635. "invalidation\n",
  1636. (unsigned long long)drhd->reg_base_addr);
  1637. } else {
  1638. iommu->flush.flush_context = qi_flush_context;
  1639. iommu->flush.flush_iotlb = qi_flush_iotlb;
  1640. printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
  1641. "invalidation\n",
  1642. (unsigned long long)drhd->reg_base_addr);
  1643. }
  1644. }
  1645. #ifdef CONFIG_INTR_REMAP
  1646. if (!intr_remapping_enabled) {
  1647. ret = enable_intr_remapping(0);
  1648. if (ret)
  1649. printk(KERN_ERR
  1650. "IOMMU: enable interrupt remapping failed\n");
  1651. }
  1652. #endif
  1653. /*
  1654. * For each rmrr
  1655. * for each dev attached to rmrr
  1656. * do
  1657. * locate drhd for dev, alloc domain for dev
  1658. * allocate free domain
  1659. * allocate page table entries for rmrr
  1660. * if context not allocated for bus
  1661. * allocate and init context
  1662. * set present in root table for this bus
  1663. * init context with domain, translation etc
  1664. * endfor
  1665. * endfor
  1666. */
  1667. for_each_rmrr_units(rmrr) {
  1668. for (i = 0; i < rmrr->devices_cnt; i++) {
  1669. pdev = rmrr->devices[i];
  1670. /* some BIOS lists non-exist devices in DMAR table */
  1671. if (!pdev)
  1672. continue;
  1673. ret = iommu_prepare_rmrr_dev(rmrr, pdev);
  1674. if (ret)
  1675. printk(KERN_ERR
  1676. "IOMMU: mapping reserved region failed\n");
  1677. }
  1678. }
  1679. iommu_prepare_gfx_mapping();
  1680. iommu_prepare_isa();
  1681. /*
  1682. * for each drhd
  1683. * enable fault log
  1684. * global invalidate context cache
  1685. * global invalidate iotlb
  1686. * enable translation
  1687. */
  1688. for_each_drhd_unit(drhd) {
  1689. if (drhd->ignored)
  1690. continue;
  1691. iommu = drhd->iommu;
  1692. iommu_flush_write_buffer(iommu);
  1693. ret = dmar_set_interrupt(iommu);
  1694. if (ret)
  1695. goto error;
  1696. iommu_set_root_entry(iommu);
  1697. iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
  1698. 0);
  1699. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
  1700. 0);
  1701. iommu_disable_protect_mem_regions(iommu);
  1702. ret = iommu_enable_translation(iommu);
  1703. if (ret)
  1704. goto error;
  1705. }
  1706. return 0;
  1707. error:
  1708. for_each_drhd_unit(drhd) {
  1709. if (drhd->ignored)
  1710. continue;
  1711. iommu = drhd->iommu;
  1712. free_iommu(iommu);
  1713. }
  1714. kfree(g_iommus);
  1715. return ret;
  1716. }
  1717. static inline u64 aligned_size(u64 host_addr, size_t size)
  1718. {
  1719. u64 addr;
  1720. addr = (host_addr & (~PAGE_MASK)) + size;
  1721. return PAGE_ALIGN(addr);
  1722. }
  1723. struct iova *
  1724. iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
  1725. {
  1726. struct iova *piova;
  1727. /* Make sure it's in range */
  1728. end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
  1729. if (!size || (IOVA_START_ADDR + size > end))
  1730. return NULL;
  1731. piova = alloc_iova(&domain->iovad,
  1732. size >> PAGE_SHIFT, IOVA_PFN(end), 1);
  1733. return piova;
  1734. }
  1735. static struct iova *
  1736. __intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
  1737. size_t size, u64 dma_mask)
  1738. {
  1739. struct pci_dev *pdev = to_pci_dev(dev);
  1740. struct iova *iova = NULL;
  1741. if (dma_mask <= DMA_32BIT_MASK || dmar_forcedac)
  1742. iova = iommu_alloc_iova(domain, size, dma_mask);
  1743. else {
  1744. /*
  1745. * First try to allocate an io virtual address in
  1746. * DMA_32BIT_MASK and if that fails then try allocating
  1747. * from higher range
  1748. */
  1749. iova = iommu_alloc_iova(domain, size, DMA_32BIT_MASK);
  1750. if (!iova)
  1751. iova = iommu_alloc_iova(domain, size, dma_mask);
  1752. }
  1753. if (!iova) {
  1754. printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
  1755. return NULL;
  1756. }
  1757. return iova;
  1758. }
  1759. static struct dmar_domain *
  1760. get_valid_domain_for_dev(struct pci_dev *pdev)
  1761. {
  1762. struct dmar_domain *domain;
  1763. int ret;
  1764. domain = get_domain_for_dev(pdev,
  1765. DEFAULT_DOMAIN_ADDRESS_WIDTH);
  1766. if (!domain) {
  1767. printk(KERN_ERR
  1768. "Allocating domain for %s failed", pci_name(pdev));
  1769. return NULL;
  1770. }
  1771. /* make sure context mapping is ok */
  1772. if (unlikely(!domain_context_mapped(pdev))) {
  1773. ret = domain_context_mapping(domain, pdev);
  1774. if (ret) {
  1775. printk(KERN_ERR
  1776. "Domain context map for %s failed",
  1777. pci_name(pdev));
  1778. return NULL;
  1779. }
  1780. }
  1781. return domain;
  1782. }
  1783. static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
  1784. size_t size, int dir, u64 dma_mask)
  1785. {
  1786. struct pci_dev *pdev = to_pci_dev(hwdev);
  1787. struct dmar_domain *domain;
  1788. phys_addr_t start_paddr;
  1789. struct iova *iova;
  1790. int prot = 0;
  1791. int ret;
  1792. struct intel_iommu *iommu;
  1793. BUG_ON(dir == DMA_NONE);
  1794. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  1795. return paddr;
  1796. domain = get_valid_domain_for_dev(pdev);
  1797. if (!domain)
  1798. return 0;
  1799. iommu = domain_get_iommu(domain);
  1800. size = aligned_size((u64)paddr, size);
  1801. iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
  1802. if (!iova)
  1803. goto error;
  1804. start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
  1805. /*
  1806. * Check if DMAR supports zero-length reads on write only
  1807. * mappings..
  1808. */
  1809. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
  1810. !cap_zlr(iommu->cap))
  1811. prot |= DMA_PTE_READ;
  1812. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  1813. prot |= DMA_PTE_WRITE;
  1814. /*
  1815. * paddr - (paddr + size) might be partial page, we should map the whole
  1816. * page. Note: if two part of one page are separately mapped, we
  1817. * might have two guest_addr mapping to the same host paddr, but this
  1818. * is not a big problem
  1819. */
  1820. ret = domain_page_mapping(domain, start_paddr,
  1821. ((u64)paddr) & PAGE_MASK, size, prot);
  1822. if (ret)
  1823. goto error;
  1824. /* it's a non-present to present mapping */
  1825. ret = iommu_flush_iotlb_psi(iommu, domain->id,
  1826. start_paddr, size >> VTD_PAGE_SHIFT, 1);
  1827. if (ret)
  1828. iommu_flush_write_buffer(iommu);
  1829. return start_paddr + ((u64)paddr & (~PAGE_MASK));
  1830. error:
  1831. if (iova)
  1832. __free_iova(&domain->iovad, iova);
  1833. printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
  1834. pci_name(pdev), size, (unsigned long long)paddr, dir);
  1835. return 0;
  1836. }
  1837. static dma_addr_t intel_map_page(struct device *dev, struct page *page,
  1838. unsigned long offset, size_t size,
  1839. enum dma_data_direction dir,
  1840. struct dma_attrs *attrs)
  1841. {
  1842. return __intel_map_single(dev, page_to_phys(page) + offset, size,
  1843. dir, to_pci_dev(dev)->dma_mask);
  1844. }
  1845. static void flush_unmaps(void)
  1846. {
  1847. int i, j;
  1848. timer_on = 0;
  1849. /* just flush them all */
  1850. for (i = 0; i < g_num_of_iommus; i++) {
  1851. struct intel_iommu *iommu = g_iommus[i];
  1852. if (!iommu)
  1853. continue;
  1854. if (deferred_flush[i].next) {
  1855. iommu->flush.flush_iotlb(iommu, 0, 0, 0,
  1856. DMA_TLB_GLOBAL_FLUSH, 0);
  1857. for (j = 0; j < deferred_flush[i].next; j++) {
  1858. __free_iova(&deferred_flush[i].domain[j]->iovad,
  1859. deferred_flush[i].iova[j]);
  1860. }
  1861. deferred_flush[i].next = 0;
  1862. }
  1863. }
  1864. list_size = 0;
  1865. }
  1866. static void flush_unmaps_timeout(unsigned long data)
  1867. {
  1868. unsigned long flags;
  1869. spin_lock_irqsave(&async_umap_flush_lock, flags);
  1870. flush_unmaps();
  1871. spin_unlock_irqrestore(&async_umap_flush_lock, flags);
  1872. }
  1873. static void add_unmap(struct dmar_domain *dom, struct iova *iova)
  1874. {
  1875. unsigned long flags;
  1876. int next, iommu_id;
  1877. struct intel_iommu *iommu;
  1878. spin_lock_irqsave(&async_umap_flush_lock, flags);
  1879. if (list_size == HIGH_WATER_MARK)
  1880. flush_unmaps();
  1881. iommu = domain_get_iommu(dom);
  1882. iommu_id = iommu->seq_id;
  1883. next = deferred_flush[iommu_id].next;
  1884. deferred_flush[iommu_id].domain[next] = dom;
  1885. deferred_flush[iommu_id].iova[next] = iova;
  1886. deferred_flush[iommu_id].next++;
  1887. if (!timer_on) {
  1888. mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
  1889. timer_on = 1;
  1890. }
  1891. list_size++;
  1892. spin_unlock_irqrestore(&async_umap_flush_lock, flags);
  1893. }
  1894. static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
  1895. size_t size, enum dma_data_direction dir,
  1896. struct dma_attrs *attrs)
  1897. {
  1898. struct pci_dev *pdev = to_pci_dev(dev);
  1899. struct dmar_domain *domain;
  1900. unsigned long start_addr;
  1901. struct iova *iova;
  1902. struct intel_iommu *iommu;
  1903. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  1904. return;
  1905. domain = find_domain(pdev);
  1906. BUG_ON(!domain);
  1907. iommu = domain_get_iommu(domain);
  1908. iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
  1909. if (!iova)
  1910. return;
  1911. start_addr = iova->pfn_lo << PAGE_SHIFT;
  1912. size = aligned_size((u64)dev_addr, size);
  1913. pr_debug("Device %s unmapping: %zx@%llx\n",
  1914. pci_name(pdev), size, (unsigned long long)start_addr);
  1915. /* clear the whole page */
  1916. dma_pte_clear_range(domain, start_addr, start_addr + size);
  1917. /* free page tables */
  1918. dma_pte_free_pagetable(domain, start_addr, start_addr + size);
  1919. if (intel_iommu_strict) {
  1920. if (iommu_flush_iotlb_psi(iommu,
  1921. domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
  1922. iommu_flush_write_buffer(iommu);
  1923. /* free iova */
  1924. __free_iova(&domain->iovad, iova);
  1925. } else {
  1926. add_unmap(domain, iova);
  1927. /*
  1928. * queue up the release of the unmap to save the 1/6th of the
  1929. * cpu used up by the iotlb flush operation...
  1930. */
  1931. }
  1932. }
  1933. static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
  1934. int dir)
  1935. {
  1936. intel_unmap_page(dev, dev_addr, size, dir, NULL);
  1937. }
  1938. static void *intel_alloc_coherent(struct device *hwdev, size_t size,
  1939. dma_addr_t *dma_handle, gfp_t flags)
  1940. {
  1941. void *vaddr;
  1942. int order;
  1943. size = PAGE_ALIGN(size);
  1944. order = get_order(size);
  1945. flags &= ~(GFP_DMA | GFP_DMA32);
  1946. vaddr = (void *)__get_free_pages(flags, order);
  1947. if (!vaddr)
  1948. return NULL;
  1949. memset(vaddr, 0, size);
  1950. *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
  1951. DMA_BIDIRECTIONAL,
  1952. hwdev->coherent_dma_mask);
  1953. if (*dma_handle)
  1954. return vaddr;
  1955. free_pages((unsigned long)vaddr, order);
  1956. return NULL;
  1957. }
  1958. static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
  1959. dma_addr_t dma_handle)
  1960. {
  1961. int order;
  1962. size = PAGE_ALIGN(size);
  1963. order = get_order(size);
  1964. intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
  1965. free_pages((unsigned long)vaddr, order);
  1966. }
  1967. static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
  1968. int nelems, enum dma_data_direction dir,
  1969. struct dma_attrs *attrs)
  1970. {
  1971. int i;
  1972. struct pci_dev *pdev = to_pci_dev(hwdev);
  1973. struct dmar_domain *domain;
  1974. unsigned long start_addr;
  1975. struct iova *iova;
  1976. size_t size = 0;
  1977. phys_addr_t addr;
  1978. struct scatterlist *sg;
  1979. struct intel_iommu *iommu;
  1980. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  1981. return;
  1982. domain = find_domain(pdev);
  1983. BUG_ON(!domain);
  1984. iommu = domain_get_iommu(domain);
  1985. iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
  1986. if (!iova)
  1987. return;
  1988. for_each_sg(sglist, sg, nelems, i) {
  1989. addr = page_to_phys(sg_page(sg)) + sg->offset;
  1990. size += aligned_size((u64)addr, sg->length);
  1991. }
  1992. start_addr = iova->pfn_lo << PAGE_SHIFT;
  1993. /* clear the whole page */
  1994. dma_pte_clear_range(domain, start_addr, start_addr + size);
  1995. /* free page tables */
  1996. dma_pte_free_pagetable(domain, start_addr, start_addr + size);
  1997. if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
  1998. size >> VTD_PAGE_SHIFT, 0))
  1999. iommu_flush_write_buffer(iommu);
  2000. /* free iova */
  2001. __free_iova(&domain->iovad, iova);
  2002. }
  2003. static int intel_nontranslate_map_sg(struct device *hddev,
  2004. struct scatterlist *sglist, int nelems, int dir)
  2005. {
  2006. int i;
  2007. struct scatterlist *sg;
  2008. for_each_sg(sglist, sg, nelems, i) {
  2009. BUG_ON(!sg_page(sg));
  2010. sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
  2011. sg->dma_length = sg->length;
  2012. }
  2013. return nelems;
  2014. }
  2015. static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
  2016. enum dma_data_direction dir, struct dma_attrs *attrs)
  2017. {
  2018. phys_addr_t addr;
  2019. int i;
  2020. struct pci_dev *pdev = to_pci_dev(hwdev);
  2021. struct dmar_domain *domain;
  2022. size_t size = 0;
  2023. int prot = 0;
  2024. size_t offset = 0;
  2025. struct iova *iova = NULL;
  2026. int ret;
  2027. struct scatterlist *sg;
  2028. unsigned long start_addr;
  2029. struct intel_iommu *iommu;
  2030. BUG_ON(dir == DMA_NONE);
  2031. if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  2032. return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
  2033. domain = get_valid_domain_for_dev(pdev);
  2034. if (!domain)
  2035. return 0;
  2036. iommu = domain_get_iommu(domain);
  2037. for_each_sg(sglist, sg, nelems, i) {
  2038. addr = page_to_phys(sg_page(sg)) + sg->offset;
  2039. size += aligned_size((u64)addr, sg->length);
  2040. }
  2041. iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
  2042. if (!iova) {
  2043. sglist->dma_length = 0;
  2044. return 0;
  2045. }
  2046. /*
  2047. * Check if DMAR supports zero-length reads on write only
  2048. * mappings..
  2049. */
  2050. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
  2051. !cap_zlr(iommu->cap))
  2052. prot |= DMA_PTE_READ;
  2053. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  2054. prot |= DMA_PTE_WRITE;
  2055. start_addr = iova->pfn_lo << PAGE_SHIFT;
  2056. offset = 0;
  2057. for_each_sg(sglist, sg, nelems, i) {
  2058. addr = page_to_phys(sg_page(sg)) + sg->offset;
  2059. size = aligned_size((u64)addr, sg->length);
  2060. ret = domain_page_mapping(domain, start_addr + offset,
  2061. ((u64)addr) & PAGE_MASK,
  2062. size, prot);
  2063. if (ret) {
  2064. /* clear the page */
  2065. dma_pte_clear_range(domain, start_addr,
  2066. start_addr + offset);
  2067. /* free page tables */
  2068. dma_pte_free_pagetable(domain, start_addr,
  2069. start_addr + offset);
  2070. /* free iova */
  2071. __free_iova(&domain->iovad, iova);
  2072. return 0;
  2073. }
  2074. sg->dma_address = start_addr + offset +
  2075. ((u64)addr & (~PAGE_MASK));
  2076. sg->dma_length = sg->length;
  2077. offset += size;
  2078. }
  2079. /* it's a non-present to present mapping */
  2080. if (iommu_flush_iotlb_psi(iommu, domain->id,
  2081. start_addr, offset >> VTD_PAGE_SHIFT, 1))
  2082. iommu_flush_write_buffer(iommu);
  2083. return nelems;
  2084. }
  2085. static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
  2086. {
  2087. return !dma_addr;
  2088. }
  2089. struct dma_map_ops intel_dma_ops = {
  2090. .alloc_coherent = intel_alloc_coherent,
  2091. .free_coherent = intel_free_coherent,
  2092. .map_sg = intel_map_sg,
  2093. .unmap_sg = intel_unmap_sg,
  2094. .map_page = intel_map_page,
  2095. .unmap_page = intel_unmap_page,
  2096. .mapping_error = intel_mapping_error,
  2097. };
  2098. static inline int iommu_domain_cache_init(void)
  2099. {
  2100. int ret = 0;
  2101. iommu_domain_cache = kmem_cache_create("iommu_domain",
  2102. sizeof(struct dmar_domain),
  2103. 0,
  2104. SLAB_HWCACHE_ALIGN,
  2105. NULL);
  2106. if (!iommu_domain_cache) {
  2107. printk(KERN_ERR "Couldn't create iommu_domain cache\n");
  2108. ret = -ENOMEM;
  2109. }
  2110. return ret;
  2111. }
  2112. static inline int iommu_devinfo_cache_init(void)
  2113. {
  2114. int ret = 0;
  2115. iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
  2116. sizeof(struct device_domain_info),
  2117. 0,
  2118. SLAB_HWCACHE_ALIGN,
  2119. NULL);
  2120. if (!iommu_devinfo_cache) {
  2121. printk(KERN_ERR "Couldn't create devinfo cache\n");
  2122. ret = -ENOMEM;
  2123. }
  2124. return ret;
  2125. }
  2126. static inline int iommu_iova_cache_init(void)
  2127. {
  2128. int ret = 0;
  2129. iommu_iova_cache = kmem_cache_create("iommu_iova",
  2130. sizeof(struct iova),
  2131. 0,
  2132. SLAB_HWCACHE_ALIGN,
  2133. NULL);
  2134. if (!iommu_iova_cache) {
  2135. printk(KERN_ERR "Couldn't create iova cache\n");
  2136. ret = -ENOMEM;
  2137. }
  2138. return ret;
  2139. }
  2140. static int __init iommu_init_mempool(void)
  2141. {
  2142. int ret;
  2143. ret = iommu_iova_cache_init();
  2144. if (ret)
  2145. return ret;
  2146. ret = iommu_domain_cache_init();
  2147. if (ret)
  2148. goto domain_error;
  2149. ret = iommu_devinfo_cache_init();
  2150. if (!ret)
  2151. return ret;
  2152. kmem_cache_destroy(iommu_domain_cache);
  2153. domain_error:
  2154. kmem_cache_destroy(iommu_iova_cache);
  2155. return -ENOMEM;
  2156. }
  2157. static void __init iommu_exit_mempool(void)
  2158. {
  2159. kmem_cache_destroy(iommu_devinfo_cache);
  2160. kmem_cache_destroy(iommu_domain_cache);
  2161. kmem_cache_destroy(iommu_iova_cache);
  2162. }
  2163. static void __init init_no_remapping_devices(void)
  2164. {
  2165. struct dmar_drhd_unit *drhd;
  2166. for_each_drhd_unit(drhd) {
  2167. if (!drhd->include_all) {
  2168. int i;
  2169. for (i = 0; i < drhd->devices_cnt; i++)
  2170. if (drhd->devices[i] != NULL)
  2171. break;
  2172. /* ignore DMAR unit if no pci devices exist */
  2173. if (i == drhd->devices_cnt)
  2174. drhd->ignored = 1;
  2175. }
  2176. }
  2177. if (dmar_map_gfx)
  2178. return;
  2179. for_each_drhd_unit(drhd) {
  2180. int i;
  2181. if (drhd->ignored || drhd->include_all)
  2182. continue;
  2183. for (i = 0; i < drhd->devices_cnt; i++)
  2184. if (drhd->devices[i] &&
  2185. !IS_GFX_DEVICE(drhd->devices[i]))
  2186. break;
  2187. if (i < drhd->devices_cnt)
  2188. continue;
  2189. /* bypass IOMMU if it is just for gfx devices */
  2190. drhd->ignored = 1;
  2191. for (i = 0; i < drhd->devices_cnt; i++) {
  2192. if (!drhd->devices[i])
  2193. continue;
  2194. drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
  2195. }
  2196. }
  2197. }
  2198. #ifdef CONFIG_SUSPEND
  2199. static int init_iommu_hw(void)
  2200. {
  2201. struct dmar_drhd_unit *drhd;
  2202. struct intel_iommu *iommu = NULL;
  2203. for_each_active_iommu(iommu, drhd)
  2204. if (iommu->qi)
  2205. dmar_reenable_qi(iommu);
  2206. for_each_active_iommu(iommu, drhd) {
  2207. iommu_flush_write_buffer(iommu);
  2208. iommu_set_root_entry(iommu);
  2209. iommu->flush.flush_context(iommu, 0, 0, 0,
  2210. DMA_CCMD_GLOBAL_INVL, 0);
  2211. iommu->flush.flush_iotlb(iommu, 0, 0, 0,
  2212. DMA_TLB_GLOBAL_FLUSH, 0);
  2213. iommu_disable_protect_mem_regions(iommu);
  2214. iommu_enable_translation(iommu);
  2215. }
  2216. return 0;
  2217. }
  2218. static void iommu_flush_all(void)
  2219. {
  2220. struct dmar_drhd_unit *drhd;
  2221. struct intel_iommu *iommu;
  2222. for_each_active_iommu(iommu, drhd) {
  2223. iommu->flush.flush_context(iommu, 0, 0, 0,
  2224. DMA_CCMD_GLOBAL_INVL, 0);
  2225. iommu->flush.flush_iotlb(iommu, 0, 0, 0,
  2226. DMA_TLB_GLOBAL_FLUSH, 0);
  2227. }
  2228. }
  2229. static int iommu_suspend(struct sys_device *dev, pm_message_t state)
  2230. {
  2231. struct dmar_drhd_unit *drhd;
  2232. struct intel_iommu *iommu = NULL;
  2233. unsigned long flag;
  2234. for_each_active_iommu(iommu, drhd) {
  2235. iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
  2236. GFP_ATOMIC);
  2237. if (!iommu->iommu_state)
  2238. goto nomem;
  2239. }
  2240. iommu_flush_all();
  2241. for_each_active_iommu(iommu, drhd) {
  2242. iommu_disable_translation(iommu);
  2243. spin_lock_irqsave(&iommu->register_lock, flag);
  2244. iommu->iommu_state[SR_DMAR_FECTL_REG] =
  2245. readl(iommu->reg + DMAR_FECTL_REG);
  2246. iommu->iommu_state[SR_DMAR_FEDATA_REG] =
  2247. readl(iommu->reg + DMAR_FEDATA_REG);
  2248. iommu->iommu_state[SR_DMAR_FEADDR_REG] =
  2249. readl(iommu->reg + DMAR_FEADDR_REG);
  2250. iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
  2251. readl(iommu->reg + DMAR_FEUADDR_REG);
  2252. spin_unlock_irqrestore(&iommu->register_lock, flag);
  2253. }
  2254. return 0;
  2255. nomem:
  2256. for_each_active_iommu(iommu, drhd)
  2257. kfree(iommu->iommu_state);
  2258. return -ENOMEM;
  2259. }
  2260. static int iommu_resume(struct sys_device *dev)
  2261. {
  2262. struct dmar_drhd_unit *drhd;
  2263. struct intel_iommu *iommu = NULL;
  2264. unsigned long flag;
  2265. if (init_iommu_hw()) {
  2266. WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
  2267. return -EIO;
  2268. }
  2269. for_each_active_iommu(iommu, drhd) {
  2270. spin_lock_irqsave(&iommu->register_lock, flag);
  2271. writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
  2272. iommu->reg + DMAR_FECTL_REG);
  2273. writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
  2274. iommu->reg + DMAR_FEDATA_REG);
  2275. writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
  2276. iommu->reg + DMAR_FEADDR_REG);
  2277. writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
  2278. iommu->reg + DMAR_FEUADDR_REG);
  2279. spin_unlock_irqrestore(&iommu->register_lock, flag);
  2280. }
  2281. for_each_active_iommu(iommu, drhd)
  2282. kfree(iommu->iommu_state);
  2283. return 0;
  2284. }
  2285. static struct sysdev_class iommu_sysclass = {
  2286. .name = "iommu",
  2287. .resume = iommu_resume,
  2288. .suspend = iommu_suspend,
  2289. };
  2290. static struct sys_device device_iommu = {
  2291. .cls = &iommu_sysclass,
  2292. };
  2293. static int __init init_iommu_sysfs(void)
  2294. {
  2295. int error;
  2296. error = sysdev_class_register(&iommu_sysclass);
  2297. if (error)
  2298. return error;
  2299. error = sysdev_register(&device_iommu);
  2300. if (error)
  2301. sysdev_class_unregister(&iommu_sysclass);
  2302. return error;
  2303. }
  2304. #else
  2305. static int __init init_iommu_sysfs(void)
  2306. {
  2307. return 0;
  2308. }
  2309. #endif /* CONFIG_PM */
  2310. int __init intel_iommu_init(void)
  2311. {
  2312. int ret = 0;
  2313. if (dmar_table_init())
  2314. return -ENODEV;
  2315. if (dmar_dev_scope_init())
  2316. return -ENODEV;
  2317. /*
  2318. * Check the need for DMA-remapping initialization now.
  2319. * Above initialization will also be used by Interrupt-remapping.
  2320. */
  2321. if (no_iommu || swiotlb || dmar_disabled)
  2322. return -ENODEV;
  2323. iommu_init_mempool();
  2324. dmar_init_reserved_ranges();
  2325. init_no_remapping_devices();
  2326. ret = init_dmars();
  2327. if (ret) {
  2328. printk(KERN_ERR "IOMMU: dmar init failed\n");
  2329. put_iova_domain(&reserved_iova_list);
  2330. iommu_exit_mempool();
  2331. return ret;
  2332. }
  2333. printk(KERN_INFO
  2334. "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
  2335. init_timer(&unmap_timer);
  2336. force_iommu = 1;
  2337. dma_ops = &intel_dma_ops;
  2338. init_iommu_sysfs();
  2339. register_iommu(&intel_iommu_ops);
  2340. return 0;
  2341. }
  2342. static int vm_domain_add_dev_info(struct dmar_domain *domain,
  2343. struct pci_dev *pdev)
  2344. {
  2345. struct device_domain_info *info;
  2346. unsigned long flags;
  2347. info = alloc_devinfo_mem();
  2348. if (!info)
  2349. return -ENOMEM;
  2350. info->bus = pdev->bus->number;
  2351. info->devfn = pdev->devfn;
  2352. info->dev = pdev;
  2353. info->domain = domain;
  2354. spin_lock_irqsave(&device_domain_lock, flags);
  2355. list_add(&info->link, &domain->devices);
  2356. list_add(&info->global, &device_domain_list);
  2357. pdev->dev.archdata.iommu = info;
  2358. spin_unlock_irqrestore(&device_domain_lock, flags);
  2359. return 0;
  2360. }
  2361. static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
  2362. struct pci_dev *pdev)
  2363. {
  2364. struct pci_dev *tmp, *parent;
  2365. if (!iommu || !pdev)
  2366. return;
  2367. /* dependent device detach */
  2368. tmp = pci_find_upstream_pcie_bridge(pdev);
  2369. /* Secondary interface's bus number and devfn 0 */
  2370. if (tmp) {
  2371. parent = pdev->bus->self;
  2372. while (parent != tmp) {
  2373. iommu_detach_dev(iommu, parent->bus->number,
  2374. parent->devfn);
  2375. parent = parent->bus->self;
  2376. }
  2377. if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
  2378. iommu_detach_dev(iommu,
  2379. tmp->subordinate->number, 0);
  2380. else /* this is a legacy PCI bridge */
  2381. iommu_detach_dev(iommu,
  2382. tmp->bus->number, tmp->devfn);
  2383. }
  2384. }
  2385. static void vm_domain_remove_one_dev_info(struct dmar_domain *domain,
  2386. struct pci_dev *pdev)
  2387. {
  2388. struct device_domain_info *info;
  2389. struct intel_iommu *iommu;
  2390. unsigned long flags;
  2391. int found = 0;
  2392. struct list_head *entry, *tmp;
  2393. iommu = device_to_iommu(pdev->bus->number, pdev->devfn);
  2394. if (!iommu)
  2395. return;
  2396. spin_lock_irqsave(&device_domain_lock, flags);
  2397. list_for_each_safe(entry, tmp, &domain->devices) {
  2398. info = list_entry(entry, struct device_domain_info, link);
  2399. if (info->bus == pdev->bus->number &&
  2400. info->devfn == pdev->devfn) {
  2401. list_del(&info->link);
  2402. list_del(&info->global);
  2403. if (info->dev)
  2404. info->dev->dev.archdata.iommu = NULL;
  2405. spin_unlock_irqrestore(&device_domain_lock, flags);
  2406. iommu_detach_dev(iommu, info->bus, info->devfn);
  2407. iommu_detach_dependent_devices(iommu, pdev);
  2408. free_devinfo_mem(info);
  2409. spin_lock_irqsave(&device_domain_lock, flags);
  2410. if (found)
  2411. break;
  2412. else
  2413. continue;
  2414. }
  2415. /* if there is no other devices under the same iommu
  2416. * owned by this domain, clear this iommu in iommu_bmp
  2417. * update iommu count and coherency
  2418. */
  2419. if (device_to_iommu(info->bus, info->devfn) == iommu)
  2420. found = 1;
  2421. }
  2422. if (found == 0) {
  2423. unsigned long tmp_flags;
  2424. spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
  2425. clear_bit(iommu->seq_id, &domain->iommu_bmp);
  2426. domain->iommu_count--;
  2427. domain_update_iommu_cap(domain);
  2428. spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
  2429. }
  2430. spin_unlock_irqrestore(&device_domain_lock, flags);
  2431. }
  2432. static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
  2433. {
  2434. struct device_domain_info *info;
  2435. struct intel_iommu *iommu;
  2436. unsigned long flags1, flags2;
  2437. spin_lock_irqsave(&device_domain_lock, flags1);
  2438. while (!list_empty(&domain->devices)) {
  2439. info = list_entry(domain->devices.next,
  2440. struct device_domain_info, link);
  2441. list_del(&info->link);
  2442. list_del(&info->global);
  2443. if (info->dev)
  2444. info->dev->dev.archdata.iommu = NULL;
  2445. spin_unlock_irqrestore(&device_domain_lock, flags1);
  2446. iommu = device_to_iommu(info->bus, info->devfn);
  2447. iommu_detach_dev(iommu, info->bus, info->devfn);
  2448. iommu_detach_dependent_devices(iommu, info->dev);
  2449. /* clear this iommu in iommu_bmp, update iommu count
  2450. * and capabilities
  2451. */
  2452. spin_lock_irqsave(&domain->iommu_lock, flags2);
  2453. if (test_and_clear_bit(iommu->seq_id,
  2454. &domain->iommu_bmp)) {
  2455. domain->iommu_count--;
  2456. domain_update_iommu_cap(domain);
  2457. }
  2458. spin_unlock_irqrestore(&domain->iommu_lock, flags2);
  2459. free_devinfo_mem(info);
  2460. spin_lock_irqsave(&device_domain_lock, flags1);
  2461. }
  2462. spin_unlock_irqrestore(&device_domain_lock, flags1);
  2463. }
  2464. /* domain id for virtual machine, it won't be set in context */
  2465. static unsigned long vm_domid;
  2466. static int vm_domain_min_agaw(struct dmar_domain *domain)
  2467. {
  2468. int i;
  2469. int min_agaw = domain->agaw;
  2470. i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
  2471. for (; i < g_num_of_iommus; ) {
  2472. if (min_agaw > g_iommus[i]->agaw)
  2473. min_agaw = g_iommus[i]->agaw;
  2474. i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
  2475. }
  2476. return min_agaw;
  2477. }
  2478. static struct dmar_domain *iommu_alloc_vm_domain(void)
  2479. {
  2480. struct dmar_domain *domain;
  2481. domain = alloc_domain_mem();
  2482. if (!domain)
  2483. return NULL;
  2484. domain->id = vm_domid++;
  2485. memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
  2486. domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
  2487. return domain;
  2488. }
  2489. static int vm_domain_init(struct dmar_domain *domain, int guest_width)
  2490. {
  2491. int adjust_width;
  2492. init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
  2493. spin_lock_init(&domain->mapping_lock);
  2494. spin_lock_init(&domain->iommu_lock);
  2495. domain_reserve_special_ranges(domain);
  2496. /* calculate AGAW */
  2497. domain->gaw = guest_width;
  2498. adjust_width = guestwidth_to_adjustwidth(guest_width);
  2499. domain->agaw = width_to_agaw(adjust_width);
  2500. INIT_LIST_HEAD(&domain->devices);
  2501. domain->iommu_count = 0;
  2502. domain->iommu_coherency = 0;
  2503. domain->max_addr = 0;
  2504. /* always allocate the top pgd */
  2505. domain->pgd = (struct dma_pte *)alloc_pgtable_page();
  2506. if (!domain->pgd)
  2507. return -ENOMEM;
  2508. domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
  2509. return 0;
  2510. }
  2511. static void iommu_free_vm_domain(struct dmar_domain *domain)
  2512. {
  2513. unsigned long flags;
  2514. struct dmar_drhd_unit *drhd;
  2515. struct intel_iommu *iommu;
  2516. unsigned long i;
  2517. unsigned long ndomains;
  2518. for_each_drhd_unit(drhd) {
  2519. if (drhd->ignored)
  2520. continue;
  2521. iommu = drhd->iommu;
  2522. ndomains = cap_ndoms(iommu->cap);
  2523. i = find_first_bit(iommu->domain_ids, ndomains);
  2524. for (; i < ndomains; ) {
  2525. if (iommu->domains[i] == domain) {
  2526. spin_lock_irqsave(&iommu->lock, flags);
  2527. clear_bit(i, iommu->domain_ids);
  2528. iommu->domains[i] = NULL;
  2529. spin_unlock_irqrestore(&iommu->lock, flags);
  2530. break;
  2531. }
  2532. i = find_next_bit(iommu->domain_ids, ndomains, i+1);
  2533. }
  2534. }
  2535. }
  2536. static void vm_domain_exit(struct dmar_domain *domain)
  2537. {
  2538. u64 end;
  2539. /* Domain 0 is reserved, so dont process it */
  2540. if (!domain)
  2541. return;
  2542. vm_domain_remove_all_dev_info(domain);
  2543. /* destroy iovas */
  2544. put_iova_domain(&domain->iovad);
  2545. end = DOMAIN_MAX_ADDR(domain->gaw);
  2546. end = end & (~VTD_PAGE_MASK);
  2547. /* clear ptes */
  2548. dma_pte_clear_range(domain, 0, end);
  2549. /* free page tables */
  2550. dma_pte_free_pagetable(domain, 0, end);
  2551. iommu_free_vm_domain(domain);
  2552. free_domain_mem(domain);
  2553. }
  2554. static int intel_iommu_domain_init(struct iommu_domain *domain)
  2555. {
  2556. struct dmar_domain *dmar_domain;
  2557. dmar_domain = iommu_alloc_vm_domain();
  2558. if (!dmar_domain) {
  2559. printk(KERN_ERR
  2560. "intel_iommu_domain_init: dmar_domain == NULL\n");
  2561. return -ENOMEM;
  2562. }
  2563. if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
  2564. printk(KERN_ERR
  2565. "intel_iommu_domain_init() failed\n");
  2566. vm_domain_exit(dmar_domain);
  2567. return -ENOMEM;
  2568. }
  2569. domain->priv = dmar_domain;
  2570. return 0;
  2571. }
  2572. static void intel_iommu_domain_destroy(struct iommu_domain *domain)
  2573. {
  2574. struct dmar_domain *dmar_domain = domain->priv;
  2575. domain->priv = NULL;
  2576. vm_domain_exit(dmar_domain);
  2577. }
  2578. static int intel_iommu_attach_device(struct iommu_domain *domain,
  2579. struct device *dev)
  2580. {
  2581. struct dmar_domain *dmar_domain = domain->priv;
  2582. struct pci_dev *pdev = to_pci_dev(dev);
  2583. struct intel_iommu *iommu;
  2584. int addr_width;
  2585. u64 end;
  2586. int ret;
  2587. /* normally pdev is not mapped */
  2588. if (unlikely(domain_context_mapped(pdev))) {
  2589. struct dmar_domain *old_domain;
  2590. old_domain = find_domain(pdev);
  2591. if (old_domain) {
  2592. if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
  2593. vm_domain_remove_one_dev_info(old_domain, pdev);
  2594. else
  2595. domain_remove_dev_info(old_domain);
  2596. }
  2597. }
  2598. iommu = device_to_iommu(pdev->bus->number, pdev->devfn);
  2599. if (!iommu)
  2600. return -ENODEV;
  2601. /* check if this iommu agaw is sufficient for max mapped address */
  2602. addr_width = agaw_to_width(iommu->agaw);
  2603. end = DOMAIN_MAX_ADDR(addr_width);
  2604. end = end & VTD_PAGE_MASK;
  2605. if (end < dmar_domain->max_addr) {
  2606. printk(KERN_ERR "%s: iommu agaw (%d) is not "
  2607. "sufficient for the mapped address (%llx)\n",
  2608. __func__, iommu->agaw, dmar_domain->max_addr);
  2609. return -EFAULT;
  2610. }
  2611. ret = domain_context_mapping(dmar_domain, pdev);
  2612. if (ret)
  2613. return ret;
  2614. ret = vm_domain_add_dev_info(dmar_domain, pdev);
  2615. return ret;
  2616. }
  2617. static void intel_iommu_detach_device(struct iommu_domain *domain,
  2618. struct device *dev)
  2619. {
  2620. struct dmar_domain *dmar_domain = domain->priv;
  2621. struct pci_dev *pdev = to_pci_dev(dev);
  2622. vm_domain_remove_one_dev_info(dmar_domain, pdev);
  2623. }
  2624. static int intel_iommu_map_range(struct iommu_domain *domain,
  2625. unsigned long iova, phys_addr_t hpa,
  2626. size_t size, int iommu_prot)
  2627. {
  2628. struct dmar_domain *dmar_domain = domain->priv;
  2629. u64 max_addr;
  2630. int addr_width;
  2631. int prot = 0;
  2632. int ret;
  2633. if (iommu_prot & IOMMU_READ)
  2634. prot |= DMA_PTE_READ;
  2635. if (iommu_prot & IOMMU_WRITE)
  2636. prot |= DMA_PTE_WRITE;
  2637. if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
  2638. prot |= DMA_PTE_SNP;
  2639. max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
  2640. if (dmar_domain->max_addr < max_addr) {
  2641. int min_agaw;
  2642. u64 end;
  2643. /* check if minimum agaw is sufficient for mapped address */
  2644. min_agaw = vm_domain_min_agaw(dmar_domain);
  2645. addr_width = agaw_to_width(min_agaw);
  2646. end = DOMAIN_MAX_ADDR(addr_width);
  2647. end = end & VTD_PAGE_MASK;
  2648. if (end < max_addr) {
  2649. printk(KERN_ERR "%s: iommu agaw (%d) is not "
  2650. "sufficient for the mapped address (%llx)\n",
  2651. __func__, min_agaw, max_addr);
  2652. return -EFAULT;
  2653. }
  2654. dmar_domain->max_addr = max_addr;
  2655. }
  2656. ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
  2657. return ret;
  2658. }
  2659. static void intel_iommu_unmap_range(struct iommu_domain *domain,
  2660. unsigned long iova, size_t size)
  2661. {
  2662. struct dmar_domain *dmar_domain = domain->priv;
  2663. dma_addr_t base;
  2664. /* The address might not be aligned */
  2665. base = iova & VTD_PAGE_MASK;
  2666. size = VTD_PAGE_ALIGN(size);
  2667. dma_pte_clear_range(dmar_domain, base, base + size);
  2668. if (dmar_domain->max_addr == base + size)
  2669. dmar_domain->max_addr = base;
  2670. }
  2671. static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
  2672. unsigned long iova)
  2673. {
  2674. struct dmar_domain *dmar_domain = domain->priv;
  2675. struct dma_pte *pte;
  2676. u64 phys = 0;
  2677. pte = addr_to_dma_pte(dmar_domain, iova);
  2678. if (pte)
  2679. phys = dma_pte_addr(pte);
  2680. return phys;
  2681. }
  2682. static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
  2683. unsigned long cap)
  2684. {
  2685. struct dmar_domain *dmar_domain = domain->priv;
  2686. if (cap == IOMMU_CAP_CACHE_COHERENCY)
  2687. return dmar_domain->iommu_snooping;
  2688. return 0;
  2689. }
  2690. static struct iommu_ops intel_iommu_ops = {
  2691. .domain_init = intel_iommu_domain_init,
  2692. .domain_destroy = intel_iommu_domain_destroy,
  2693. .attach_dev = intel_iommu_attach_device,
  2694. .detach_dev = intel_iommu_detach_device,
  2695. .map = intel_iommu_map_range,
  2696. .unmap = intel_iommu_unmap_range,
  2697. .iova_to_phys = intel_iommu_iova_to_phys,
  2698. .domain_has_cap = intel_iommu_domain_has_cap,
  2699. };
  2700. static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
  2701. {
  2702. /*
  2703. * Mobile 4 Series Chipset neglects to set RWBF capability,
  2704. * but needs it:
  2705. */
  2706. printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
  2707. rwbf_quirk = 1;
  2708. }
  2709. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);