intel-iommu.c 79 KB

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