intel-iommu.c 100 KB

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