intel-iommu.c 88 KB

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