intel-iommu.c 78 KB

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