intel-iommu.c 85 KB

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