intel-iommu.c 80 KB

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