intel-iommu.c 85 KB

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