intel-iommu.c 89 KB

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