intel-iommu.c 89 KB

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