intel-iommu.c 85 KB

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