intel-iommu.c 86 KB

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