intel-iommu.c 77 KB

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