intel-iommu.c 88 KB

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