intel-iommu.c 85 KB

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