intel-iommu.c 86 KB

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