intel-iommu.c 84 KB

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