intel-iommu.c 88 KB

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