intel-iommu.c 85 KB

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