intel-iommu.c 99 KB

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