intel-iommu.c 86 KB

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