intel-iommu.c 81 KB

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