intel-iommu.c 73 KB

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