intel-iommu.c 85 KB

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