intel-iommu.c 85 KB

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