intel-iommu.c 85 KB

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