intel-iommu.c 85 KB

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