intel-iommu.c 88 KB

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