amd_iommu.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/ratelimit.h>
  20. #include <linux/pci.h>
  21. #include <linux/pci-ats.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/iommu-helper.h>
  28. #include <linux/iommu.h>
  29. #include <linux/delay.h>
  30. #include <linux/amd-iommu.h>
  31. #include <linux/notifier.h>
  32. #include <linux/export.h>
  33. #include <asm/msidef.h>
  34. #include <asm/proto.h>
  35. #include <asm/iommu.h>
  36. #include <asm/gart.h>
  37. #include <asm/dma.h>
  38. #include "amd_iommu_proto.h"
  39. #include "amd_iommu_types.h"
  40. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  41. #define LOOP_TIMEOUT 100000
  42. /*
  43. * This bitmap is used to advertise the page sizes our hardware support
  44. * to the IOMMU core, which will then use this information to split
  45. * physically contiguous memory regions it is mapping into page sizes
  46. * that we support.
  47. *
  48. * Traditionally the IOMMU core just handed us the mappings directly,
  49. * after making sure the size is an order of a 4KiB page and that the
  50. * mapping has natural alignment.
  51. *
  52. * To retain this behavior, we currently advertise that we support
  53. * all page sizes that are an order of 4KiB.
  54. *
  55. * If at some point we'd like to utilize the IOMMU core's new behavior,
  56. * we could change this to advertise the real page sizes we support.
  57. */
  58. #define AMD_IOMMU_PGSIZES (~0xFFFUL)
  59. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  60. /* A list of preallocated protection domains */
  61. static LIST_HEAD(iommu_pd_list);
  62. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  63. /* List of all available dev_data structures */
  64. static LIST_HEAD(dev_data_list);
  65. static DEFINE_SPINLOCK(dev_data_list_lock);
  66. /*
  67. * Domain for untranslated devices - only allocated
  68. * if iommu=pt passed on kernel cmd line.
  69. */
  70. static struct protection_domain *pt_domain;
  71. static struct iommu_ops amd_iommu_ops;
  72. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  73. int amd_iommu_max_glx_val = -1;
  74. static struct dma_map_ops amd_iommu_dma_ops;
  75. /*
  76. * general struct to manage commands send to an IOMMU
  77. */
  78. struct iommu_cmd {
  79. u32 data[4];
  80. };
  81. static void update_domain(struct protection_domain *domain);
  82. static int __init alloc_passthrough_domain(void);
  83. /****************************************************************************
  84. *
  85. * Helper functions
  86. *
  87. ****************************************************************************/
  88. static struct iommu_dev_data *alloc_dev_data(u16 devid)
  89. {
  90. struct iommu_dev_data *dev_data;
  91. unsigned long flags;
  92. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  93. if (!dev_data)
  94. return NULL;
  95. dev_data->devid = devid;
  96. atomic_set(&dev_data->bind, 0);
  97. spin_lock_irqsave(&dev_data_list_lock, flags);
  98. list_add_tail(&dev_data->dev_data_list, &dev_data_list);
  99. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  100. return dev_data;
  101. }
  102. static void free_dev_data(struct iommu_dev_data *dev_data)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&dev_data_list_lock, flags);
  106. list_del(&dev_data->dev_data_list);
  107. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  108. kfree(dev_data);
  109. }
  110. static struct iommu_dev_data *search_dev_data(u16 devid)
  111. {
  112. struct iommu_dev_data *dev_data;
  113. unsigned long flags;
  114. spin_lock_irqsave(&dev_data_list_lock, flags);
  115. list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
  116. if (dev_data->devid == devid)
  117. goto out_unlock;
  118. }
  119. dev_data = NULL;
  120. out_unlock:
  121. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  122. return dev_data;
  123. }
  124. static struct iommu_dev_data *find_dev_data(u16 devid)
  125. {
  126. struct iommu_dev_data *dev_data;
  127. dev_data = search_dev_data(devid);
  128. if (dev_data == NULL)
  129. dev_data = alloc_dev_data(devid);
  130. return dev_data;
  131. }
  132. static inline u16 get_device_id(struct device *dev)
  133. {
  134. struct pci_dev *pdev = to_pci_dev(dev);
  135. return calc_devid(pdev->bus->number, pdev->devfn);
  136. }
  137. static struct iommu_dev_data *get_dev_data(struct device *dev)
  138. {
  139. return dev->archdata.iommu;
  140. }
  141. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  142. {
  143. static const int caps[] = {
  144. PCI_EXT_CAP_ID_ATS,
  145. PCI_EXT_CAP_ID_PRI,
  146. PCI_EXT_CAP_ID_PASID,
  147. };
  148. int i, pos;
  149. for (i = 0; i < 3; ++i) {
  150. pos = pci_find_ext_capability(pdev, caps[i]);
  151. if (pos == 0)
  152. return false;
  153. }
  154. return true;
  155. }
  156. static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
  157. {
  158. struct iommu_dev_data *dev_data;
  159. dev_data = get_dev_data(&pdev->dev);
  160. return dev_data->errata & (1 << erratum) ? true : false;
  161. }
  162. /*
  163. * In this function the list of preallocated protection domains is traversed to
  164. * find the domain for a specific device
  165. */
  166. static struct dma_ops_domain *find_protection_domain(u16 devid)
  167. {
  168. struct dma_ops_domain *entry, *ret = NULL;
  169. unsigned long flags;
  170. u16 alias = amd_iommu_alias_table[devid];
  171. if (list_empty(&iommu_pd_list))
  172. return NULL;
  173. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  174. list_for_each_entry(entry, &iommu_pd_list, list) {
  175. if (entry->target_dev == devid ||
  176. entry->target_dev == alias) {
  177. ret = entry;
  178. break;
  179. }
  180. }
  181. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  182. return ret;
  183. }
  184. /*
  185. * This function checks if the driver got a valid device from the caller to
  186. * avoid dereferencing invalid pointers.
  187. */
  188. static bool check_device(struct device *dev)
  189. {
  190. u16 devid;
  191. if (!dev || !dev->dma_mask)
  192. return false;
  193. /* No device or no PCI device */
  194. if (dev->bus != &pci_bus_type)
  195. return false;
  196. devid = get_device_id(dev);
  197. /* Out of our scope? */
  198. if (devid > amd_iommu_last_bdf)
  199. return false;
  200. if (amd_iommu_rlookup_table[devid] == NULL)
  201. return false;
  202. return true;
  203. }
  204. static int iommu_init_device(struct device *dev)
  205. {
  206. struct pci_dev *pdev = to_pci_dev(dev);
  207. struct iommu_dev_data *dev_data;
  208. u16 alias;
  209. if (dev->archdata.iommu)
  210. return 0;
  211. dev_data = find_dev_data(get_device_id(dev));
  212. if (!dev_data)
  213. return -ENOMEM;
  214. alias = amd_iommu_alias_table[dev_data->devid];
  215. if (alias != dev_data->devid) {
  216. struct iommu_dev_data *alias_data;
  217. alias_data = find_dev_data(alias);
  218. if (alias_data == NULL) {
  219. pr_err("AMD-Vi: Warning: Unhandled device %s\n",
  220. dev_name(dev));
  221. free_dev_data(dev_data);
  222. return -ENOTSUPP;
  223. }
  224. dev_data->alias_data = alias_data;
  225. }
  226. if (pci_iommuv2_capable(pdev)) {
  227. struct amd_iommu *iommu;
  228. iommu = amd_iommu_rlookup_table[dev_data->devid];
  229. dev_data->iommu_v2 = iommu->is_iommu_v2;
  230. }
  231. dev->archdata.iommu = dev_data;
  232. return 0;
  233. }
  234. static void iommu_ignore_device(struct device *dev)
  235. {
  236. u16 devid, alias;
  237. devid = get_device_id(dev);
  238. alias = amd_iommu_alias_table[devid];
  239. memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
  240. memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
  241. amd_iommu_rlookup_table[devid] = NULL;
  242. amd_iommu_rlookup_table[alias] = NULL;
  243. }
  244. static void iommu_uninit_device(struct device *dev)
  245. {
  246. /*
  247. * Nothing to do here - we keep dev_data around for unplugged devices
  248. * and reuse it when the device is re-plugged - not doing so would
  249. * introduce a ton of races.
  250. */
  251. }
  252. void __init amd_iommu_uninit_devices(void)
  253. {
  254. struct iommu_dev_data *dev_data, *n;
  255. struct pci_dev *pdev = NULL;
  256. for_each_pci_dev(pdev) {
  257. if (!check_device(&pdev->dev))
  258. continue;
  259. iommu_uninit_device(&pdev->dev);
  260. }
  261. /* Free all of our dev_data structures */
  262. list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
  263. free_dev_data(dev_data);
  264. }
  265. int __init amd_iommu_init_devices(void)
  266. {
  267. struct pci_dev *pdev = NULL;
  268. int ret = 0;
  269. for_each_pci_dev(pdev) {
  270. if (!check_device(&pdev->dev))
  271. continue;
  272. ret = iommu_init_device(&pdev->dev);
  273. if (ret == -ENOTSUPP)
  274. iommu_ignore_device(&pdev->dev);
  275. else if (ret)
  276. goto out_free;
  277. }
  278. return 0;
  279. out_free:
  280. amd_iommu_uninit_devices();
  281. return ret;
  282. }
  283. #ifdef CONFIG_AMD_IOMMU_STATS
  284. /*
  285. * Initialization code for statistics collection
  286. */
  287. DECLARE_STATS_COUNTER(compl_wait);
  288. DECLARE_STATS_COUNTER(cnt_map_single);
  289. DECLARE_STATS_COUNTER(cnt_unmap_single);
  290. DECLARE_STATS_COUNTER(cnt_map_sg);
  291. DECLARE_STATS_COUNTER(cnt_unmap_sg);
  292. DECLARE_STATS_COUNTER(cnt_alloc_coherent);
  293. DECLARE_STATS_COUNTER(cnt_free_coherent);
  294. DECLARE_STATS_COUNTER(cross_page);
  295. DECLARE_STATS_COUNTER(domain_flush_single);
  296. DECLARE_STATS_COUNTER(domain_flush_all);
  297. DECLARE_STATS_COUNTER(alloced_io_mem);
  298. DECLARE_STATS_COUNTER(total_map_requests);
  299. DECLARE_STATS_COUNTER(complete_ppr);
  300. DECLARE_STATS_COUNTER(invalidate_iotlb);
  301. DECLARE_STATS_COUNTER(invalidate_iotlb_all);
  302. DECLARE_STATS_COUNTER(pri_requests);
  303. static struct dentry *stats_dir;
  304. static struct dentry *de_fflush;
  305. static void amd_iommu_stats_add(struct __iommu_counter *cnt)
  306. {
  307. if (stats_dir == NULL)
  308. return;
  309. cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
  310. &cnt->value);
  311. }
  312. static void amd_iommu_stats_init(void)
  313. {
  314. stats_dir = debugfs_create_dir("amd-iommu", NULL);
  315. if (stats_dir == NULL)
  316. return;
  317. de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
  318. (u32 *)&amd_iommu_unmap_flush);
  319. amd_iommu_stats_add(&compl_wait);
  320. amd_iommu_stats_add(&cnt_map_single);
  321. amd_iommu_stats_add(&cnt_unmap_single);
  322. amd_iommu_stats_add(&cnt_map_sg);
  323. amd_iommu_stats_add(&cnt_unmap_sg);
  324. amd_iommu_stats_add(&cnt_alloc_coherent);
  325. amd_iommu_stats_add(&cnt_free_coherent);
  326. amd_iommu_stats_add(&cross_page);
  327. amd_iommu_stats_add(&domain_flush_single);
  328. amd_iommu_stats_add(&domain_flush_all);
  329. amd_iommu_stats_add(&alloced_io_mem);
  330. amd_iommu_stats_add(&total_map_requests);
  331. amd_iommu_stats_add(&complete_ppr);
  332. amd_iommu_stats_add(&invalidate_iotlb);
  333. amd_iommu_stats_add(&invalidate_iotlb_all);
  334. amd_iommu_stats_add(&pri_requests);
  335. }
  336. #endif
  337. /****************************************************************************
  338. *
  339. * Interrupt handling functions
  340. *
  341. ****************************************************************************/
  342. static void dump_dte_entry(u16 devid)
  343. {
  344. int i;
  345. for (i = 0; i < 4; ++i)
  346. pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
  347. amd_iommu_dev_table[devid].data[i]);
  348. }
  349. static void dump_command(unsigned long phys_addr)
  350. {
  351. struct iommu_cmd *cmd = phys_to_virt(phys_addr);
  352. int i;
  353. for (i = 0; i < 4; ++i)
  354. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  355. }
  356. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  357. {
  358. int type, devid, domid, flags;
  359. volatile u32 *event = __evt;
  360. int count = 0;
  361. u64 address;
  362. retry:
  363. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  364. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  365. domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  366. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  367. address = (u64)(((u64)event[3]) << 32) | event[2];
  368. if (type == 0) {
  369. /* Did we hit the erratum? */
  370. if (++count == LOOP_TIMEOUT) {
  371. pr_err("AMD-Vi: No event written to event log\n");
  372. return;
  373. }
  374. udelay(1);
  375. goto retry;
  376. }
  377. printk(KERN_ERR "AMD-Vi: Event logged [");
  378. switch (type) {
  379. case EVENT_TYPE_ILL_DEV:
  380. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  381. "address=0x%016llx flags=0x%04x]\n",
  382. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  383. address, flags);
  384. dump_dte_entry(devid);
  385. break;
  386. case EVENT_TYPE_IO_FAULT:
  387. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  388. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  389. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  390. domid, address, flags);
  391. break;
  392. case EVENT_TYPE_DEV_TAB_ERR:
  393. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  394. "address=0x%016llx flags=0x%04x]\n",
  395. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  396. address, flags);
  397. break;
  398. case EVENT_TYPE_PAGE_TAB_ERR:
  399. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  400. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  401. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  402. domid, address, flags);
  403. break;
  404. case EVENT_TYPE_ILL_CMD:
  405. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  406. dump_command(address);
  407. break;
  408. case EVENT_TYPE_CMD_HARD_ERR:
  409. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  410. "flags=0x%04x]\n", address, flags);
  411. break;
  412. case EVENT_TYPE_IOTLB_INV_TO:
  413. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  414. "address=0x%016llx]\n",
  415. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  416. address);
  417. break;
  418. case EVENT_TYPE_INV_DEV_REQ:
  419. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  420. "address=0x%016llx flags=0x%04x]\n",
  421. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  422. address, flags);
  423. break;
  424. default:
  425. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  426. }
  427. memset(__evt, 0, 4 * sizeof(u32));
  428. }
  429. static void iommu_poll_events(struct amd_iommu *iommu)
  430. {
  431. u32 head, tail, status;
  432. unsigned long flags;
  433. spin_lock_irqsave(&iommu->lock, flags);
  434. /* enable event interrupts again */
  435. do {
  436. /*
  437. * Workaround for Erratum ERBT1312
  438. * Clearing the EVT_INT bit may race in the hardware, so read
  439. * it again and make sure it was really cleared
  440. */
  441. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  442. writel(MMIO_STATUS_EVT_INT_MASK,
  443. iommu->mmio_base + MMIO_STATUS_OFFSET);
  444. } while (status & MMIO_STATUS_EVT_INT_MASK);
  445. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  446. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  447. while (head != tail) {
  448. iommu_print_event(iommu, iommu->evt_buf + head);
  449. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  450. }
  451. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  452. spin_unlock_irqrestore(&iommu->lock, flags);
  453. }
  454. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
  455. {
  456. struct amd_iommu_fault fault;
  457. INC_STATS_COUNTER(pri_requests);
  458. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  459. pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
  460. return;
  461. }
  462. fault.address = raw[1];
  463. fault.pasid = PPR_PASID(raw[0]);
  464. fault.device_id = PPR_DEVID(raw[0]);
  465. fault.tag = PPR_TAG(raw[0]);
  466. fault.flags = PPR_FLAGS(raw[0]);
  467. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  468. }
  469. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  470. {
  471. unsigned long flags;
  472. u32 head, tail, status;
  473. if (iommu->ppr_log == NULL)
  474. return;
  475. spin_lock_irqsave(&iommu->lock, flags);
  476. /* enable ppr interrupts again */
  477. do {
  478. /*
  479. * Workaround for Erratum ERBT1312
  480. * Clearing the PPR_INT bit may race in the hardware, so read
  481. * it again and make sure it was really cleared
  482. */
  483. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  484. writel(MMIO_STATUS_PPR_INT_MASK,
  485. iommu->mmio_base + MMIO_STATUS_OFFSET);
  486. } while (status & MMIO_STATUS_PPR_INT_MASK);
  487. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  488. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  489. while (head != tail) {
  490. volatile u64 *raw;
  491. u64 entry[2];
  492. int i;
  493. raw = (u64 *)(iommu->ppr_log + head);
  494. /*
  495. * Hardware bug: Interrupt may arrive before the entry is
  496. * written to memory. If this happens we need to wait for the
  497. * entry to arrive.
  498. */
  499. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  500. if (PPR_REQ_TYPE(raw[0]) != 0)
  501. break;
  502. udelay(1);
  503. }
  504. /* Avoid memcpy function-call overhead */
  505. entry[0] = raw[0];
  506. entry[1] = raw[1];
  507. /*
  508. * To detect the hardware bug we need to clear the entry
  509. * back to zero.
  510. */
  511. raw[0] = raw[1] = 0UL;
  512. /* Update head pointer of hardware ring-buffer */
  513. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  514. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  515. /*
  516. * Release iommu->lock because ppr-handling might need to
  517. * re-aquire it
  518. */
  519. spin_unlock_irqrestore(&iommu->lock, flags);
  520. /* Handle PPR entry */
  521. iommu_handle_ppr_entry(iommu, entry);
  522. spin_lock_irqsave(&iommu->lock, flags);
  523. /* Refresh ring-buffer information */
  524. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  525. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  526. }
  527. spin_unlock_irqrestore(&iommu->lock, flags);
  528. }
  529. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  530. {
  531. struct amd_iommu *iommu;
  532. for_each_iommu(iommu) {
  533. iommu_poll_events(iommu);
  534. iommu_poll_ppr_log(iommu);
  535. }
  536. return IRQ_HANDLED;
  537. }
  538. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  539. {
  540. return IRQ_WAKE_THREAD;
  541. }
  542. /****************************************************************************
  543. *
  544. * IOMMU command queuing functions
  545. *
  546. ****************************************************************************/
  547. static int wait_on_sem(volatile u64 *sem)
  548. {
  549. int i = 0;
  550. while (*sem == 0 && i < LOOP_TIMEOUT) {
  551. udelay(1);
  552. i += 1;
  553. }
  554. if (i == LOOP_TIMEOUT) {
  555. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  556. return -EIO;
  557. }
  558. return 0;
  559. }
  560. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  561. struct iommu_cmd *cmd,
  562. u32 tail)
  563. {
  564. u8 *target;
  565. target = iommu->cmd_buf + tail;
  566. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  567. /* Copy command to buffer */
  568. memcpy(target, cmd, sizeof(*cmd));
  569. /* Tell the IOMMU about it */
  570. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  571. }
  572. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  573. {
  574. WARN_ON(address & 0x7ULL);
  575. memset(cmd, 0, sizeof(*cmd));
  576. cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
  577. cmd->data[1] = upper_32_bits(__pa(address));
  578. cmd->data[2] = 1;
  579. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  580. }
  581. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  582. {
  583. memset(cmd, 0, sizeof(*cmd));
  584. cmd->data[0] = devid;
  585. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  586. }
  587. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  588. size_t size, u16 domid, int pde)
  589. {
  590. u64 pages;
  591. int s;
  592. pages = iommu_num_pages(address, size, PAGE_SIZE);
  593. s = 0;
  594. if (pages > 1) {
  595. /*
  596. * If we have to flush more than one page, flush all
  597. * TLB entries for this domain
  598. */
  599. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  600. s = 1;
  601. }
  602. address &= PAGE_MASK;
  603. memset(cmd, 0, sizeof(*cmd));
  604. cmd->data[1] |= domid;
  605. cmd->data[2] = lower_32_bits(address);
  606. cmd->data[3] = upper_32_bits(address);
  607. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  608. if (s) /* size bit - we flush more than one 4kb page */
  609. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  610. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  611. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  612. }
  613. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  614. u64 address, size_t size)
  615. {
  616. u64 pages;
  617. int s;
  618. pages = iommu_num_pages(address, size, PAGE_SIZE);
  619. s = 0;
  620. if (pages > 1) {
  621. /*
  622. * If we have to flush more than one page, flush all
  623. * TLB entries for this domain
  624. */
  625. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  626. s = 1;
  627. }
  628. address &= PAGE_MASK;
  629. memset(cmd, 0, sizeof(*cmd));
  630. cmd->data[0] = devid;
  631. cmd->data[0] |= (qdep & 0xff) << 24;
  632. cmd->data[1] = devid;
  633. cmd->data[2] = lower_32_bits(address);
  634. cmd->data[3] = upper_32_bits(address);
  635. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  636. if (s)
  637. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  638. }
  639. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
  640. u64 address, bool size)
  641. {
  642. memset(cmd, 0, sizeof(*cmd));
  643. address &= ~(0xfffULL);
  644. cmd->data[0] = pasid & PASID_MASK;
  645. cmd->data[1] = domid;
  646. cmd->data[2] = lower_32_bits(address);
  647. cmd->data[3] = upper_32_bits(address);
  648. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  649. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  650. if (size)
  651. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  652. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  653. }
  654. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
  655. int qdep, u64 address, bool size)
  656. {
  657. memset(cmd, 0, sizeof(*cmd));
  658. address &= ~(0xfffULL);
  659. cmd->data[0] = devid;
  660. cmd->data[0] |= (pasid & 0xff) << 16;
  661. cmd->data[0] |= (qdep & 0xff) << 24;
  662. cmd->data[1] = devid;
  663. cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
  664. cmd->data[2] = lower_32_bits(address);
  665. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  666. cmd->data[3] = upper_32_bits(address);
  667. if (size)
  668. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  669. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  670. }
  671. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
  672. int status, int tag, bool gn)
  673. {
  674. memset(cmd, 0, sizeof(*cmd));
  675. cmd->data[0] = devid;
  676. if (gn) {
  677. cmd->data[1] = pasid & PASID_MASK;
  678. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  679. }
  680. cmd->data[3] = tag & 0x1ff;
  681. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  682. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  683. }
  684. static void build_inv_all(struct iommu_cmd *cmd)
  685. {
  686. memset(cmd, 0, sizeof(*cmd));
  687. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  688. }
  689. /*
  690. * Writes the command to the IOMMUs command buffer and informs the
  691. * hardware about the new command.
  692. */
  693. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  694. struct iommu_cmd *cmd,
  695. bool sync)
  696. {
  697. u32 left, tail, head, next_tail;
  698. unsigned long flags;
  699. WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
  700. again:
  701. spin_lock_irqsave(&iommu->lock, flags);
  702. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  703. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  704. next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  705. left = (head - next_tail) % iommu->cmd_buf_size;
  706. if (left <= 2) {
  707. struct iommu_cmd sync_cmd;
  708. volatile u64 sem = 0;
  709. int ret;
  710. build_completion_wait(&sync_cmd, (u64)&sem);
  711. copy_cmd_to_buffer(iommu, &sync_cmd, tail);
  712. spin_unlock_irqrestore(&iommu->lock, flags);
  713. if ((ret = wait_on_sem(&sem)) != 0)
  714. return ret;
  715. goto again;
  716. }
  717. copy_cmd_to_buffer(iommu, cmd, tail);
  718. /* We need to sync now to make sure all commands are processed */
  719. iommu->need_sync = sync;
  720. spin_unlock_irqrestore(&iommu->lock, flags);
  721. return 0;
  722. }
  723. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  724. {
  725. return iommu_queue_command_sync(iommu, cmd, true);
  726. }
  727. /*
  728. * This function queues a completion wait command into the command
  729. * buffer of an IOMMU
  730. */
  731. static int iommu_completion_wait(struct amd_iommu *iommu)
  732. {
  733. struct iommu_cmd cmd;
  734. volatile u64 sem = 0;
  735. int ret;
  736. if (!iommu->need_sync)
  737. return 0;
  738. build_completion_wait(&cmd, (u64)&sem);
  739. ret = iommu_queue_command_sync(iommu, &cmd, false);
  740. if (ret)
  741. return ret;
  742. return wait_on_sem(&sem);
  743. }
  744. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  745. {
  746. struct iommu_cmd cmd;
  747. build_inv_dte(&cmd, devid);
  748. return iommu_queue_command(iommu, &cmd);
  749. }
  750. static void iommu_flush_dte_all(struct amd_iommu *iommu)
  751. {
  752. u32 devid;
  753. for (devid = 0; devid <= 0xffff; ++devid)
  754. iommu_flush_dte(iommu, devid);
  755. iommu_completion_wait(iommu);
  756. }
  757. /*
  758. * This function uses heavy locking and may disable irqs for some time. But
  759. * this is no issue because it is only called during resume.
  760. */
  761. static void iommu_flush_tlb_all(struct amd_iommu *iommu)
  762. {
  763. u32 dom_id;
  764. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  765. struct iommu_cmd cmd;
  766. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  767. dom_id, 1);
  768. iommu_queue_command(iommu, &cmd);
  769. }
  770. iommu_completion_wait(iommu);
  771. }
  772. static void iommu_flush_all(struct amd_iommu *iommu)
  773. {
  774. struct iommu_cmd cmd;
  775. build_inv_all(&cmd);
  776. iommu_queue_command(iommu, &cmd);
  777. iommu_completion_wait(iommu);
  778. }
  779. void iommu_flush_all_caches(struct amd_iommu *iommu)
  780. {
  781. if (iommu_feature(iommu, FEATURE_IA)) {
  782. iommu_flush_all(iommu);
  783. } else {
  784. iommu_flush_dte_all(iommu);
  785. iommu_flush_tlb_all(iommu);
  786. }
  787. }
  788. /*
  789. * Command send function for flushing on-device TLB
  790. */
  791. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  792. u64 address, size_t size)
  793. {
  794. struct amd_iommu *iommu;
  795. struct iommu_cmd cmd;
  796. int qdep;
  797. qdep = dev_data->ats.qdep;
  798. iommu = amd_iommu_rlookup_table[dev_data->devid];
  799. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  800. return iommu_queue_command(iommu, &cmd);
  801. }
  802. /*
  803. * Command send function for invalidating a device table entry
  804. */
  805. static int device_flush_dte(struct iommu_dev_data *dev_data)
  806. {
  807. struct amd_iommu *iommu;
  808. int ret;
  809. iommu = amd_iommu_rlookup_table[dev_data->devid];
  810. ret = iommu_flush_dte(iommu, dev_data->devid);
  811. if (ret)
  812. return ret;
  813. if (dev_data->ats.enabled)
  814. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  815. return ret;
  816. }
  817. /*
  818. * TLB invalidation function which is called from the mapping functions.
  819. * It invalidates a single PTE if the range to flush is within a single
  820. * page. Otherwise it flushes the whole TLB of the IOMMU.
  821. */
  822. static void __domain_flush_pages(struct protection_domain *domain,
  823. u64 address, size_t size, int pde)
  824. {
  825. struct iommu_dev_data *dev_data;
  826. struct iommu_cmd cmd;
  827. int ret = 0, i;
  828. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  829. for (i = 0; i < amd_iommus_present; ++i) {
  830. if (!domain->dev_iommu[i])
  831. continue;
  832. /*
  833. * Devices of this domain are behind this IOMMU
  834. * We need a TLB flush
  835. */
  836. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  837. }
  838. list_for_each_entry(dev_data, &domain->dev_list, list) {
  839. if (!dev_data->ats.enabled)
  840. continue;
  841. ret |= device_flush_iotlb(dev_data, address, size);
  842. }
  843. WARN_ON(ret);
  844. }
  845. static void domain_flush_pages(struct protection_domain *domain,
  846. u64 address, size_t size)
  847. {
  848. __domain_flush_pages(domain, address, size, 0);
  849. }
  850. /* Flush the whole IO/TLB for a given protection domain */
  851. static void domain_flush_tlb(struct protection_domain *domain)
  852. {
  853. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  854. }
  855. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  856. static void domain_flush_tlb_pde(struct protection_domain *domain)
  857. {
  858. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  859. }
  860. static void domain_flush_complete(struct protection_domain *domain)
  861. {
  862. int i;
  863. for (i = 0; i < amd_iommus_present; ++i) {
  864. if (!domain->dev_iommu[i])
  865. continue;
  866. /*
  867. * Devices of this domain are behind this IOMMU
  868. * We need to wait for completion of all commands.
  869. */
  870. iommu_completion_wait(amd_iommus[i]);
  871. }
  872. }
  873. /*
  874. * This function flushes the DTEs for all devices in domain
  875. */
  876. static void domain_flush_devices(struct protection_domain *domain)
  877. {
  878. struct iommu_dev_data *dev_data;
  879. list_for_each_entry(dev_data, &domain->dev_list, list)
  880. device_flush_dte(dev_data);
  881. }
  882. /****************************************************************************
  883. *
  884. * The functions below are used the create the page table mappings for
  885. * unity mapped regions.
  886. *
  887. ****************************************************************************/
  888. /*
  889. * This function is used to add another level to an IO page table. Adding
  890. * another level increases the size of the address space by 9 bits to a size up
  891. * to 64 bits.
  892. */
  893. static bool increase_address_space(struct protection_domain *domain,
  894. gfp_t gfp)
  895. {
  896. u64 *pte;
  897. if (domain->mode == PAGE_MODE_6_LEVEL)
  898. /* address space already 64 bit large */
  899. return false;
  900. pte = (void *)get_zeroed_page(gfp);
  901. if (!pte)
  902. return false;
  903. *pte = PM_LEVEL_PDE(domain->mode,
  904. virt_to_phys(domain->pt_root));
  905. domain->pt_root = pte;
  906. domain->mode += 1;
  907. domain->updated = true;
  908. return true;
  909. }
  910. static u64 *alloc_pte(struct protection_domain *domain,
  911. unsigned long address,
  912. unsigned long page_size,
  913. u64 **pte_page,
  914. gfp_t gfp)
  915. {
  916. int level, end_lvl;
  917. u64 *pte, *page;
  918. BUG_ON(!is_power_of_2(page_size));
  919. while (address > PM_LEVEL_SIZE(domain->mode))
  920. increase_address_space(domain, gfp);
  921. level = domain->mode - 1;
  922. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  923. address = PAGE_SIZE_ALIGN(address, page_size);
  924. end_lvl = PAGE_SIZE_LEVEL(page_size);
  925. while (level > end_lvl) {
  926. if (!IOMMU_PTE_PRESENT(*pte)) {
  927. page = (u64 *)get_zeroed_page(gfp);
  928. if (!page)
  929. return NULL;
  930. *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
  931. }
  932. /* No level skipping support yet */
  933. if (PM_PTE_LEVEL(*pte) != level)
  934. return NULL;
  935. level -= 1;
  936. pte = IOMMU_PTE_PAGE(*pte);
  937. if (pte_page && level == end_lvl)
  938. *pte_page = pte;
  939. pte = &pte[PM_LEVEL_INDEX(level, address)];
  940. }
  941. return pte;
  942. }
  943. /*
  944. * This function checks if there is a PTE for a given dma address. If
  945. * there is one, it returns the pointer to it.
  946. */
  947. static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
  948. {
  949. int level;
  950. u64 *pte;
  951. if (address > PM_LEVEL_SIZE(domain->mode))
  952. return NULL;
  953. level = domain->mode - 1;
  954. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  955. while (level > 0) {
  956. /* Not Present */
  957. if (!IOMMU_PTE_PRESENT(*pte))
  958. return NULL;
  959. /* Large PTE */
  960. if (PM_PTE_LEVEL(*pte) == 0x07) {
  961. unsigned long pte_mask, __pte;
  962. /*
  963. * If we have a series of large PTEs, make
  964. * sure to return a pointer to the first one.
  965. */
  966. pte_mask = PTE_PAGE_SIZE(*pte);
  967. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  968. __pte = ((unsigned long)pte) & pte_mask;
  969. return (u64 *)__pte;
  970. }
  971. /* No level skipping support yet */
  972. if (PM_PTE_LEVEL(*pte) != level)
  973. return NULL;
  974. level -= 1;
  975. /* Walk to the next level */
  976. pte = IOMMU_PTE_PAGE(*pte);
  977. pte = &pte[PM_LEVEL_INDEX(level, address)];
  978. }
  979. return pte;
  980. }
  981. /*
  982. * Generic mapping functions. It maps a physical address into a DMA
  983. * address space. It allocates the page table pages if necessary.
  984. * In the future it can be extended to a generic mapping function
  985. * supporting all features of AMD IOMMU page tables like level skipping
  986. * and full 64 bit address spaces.
  987. */
  988. static int iommu_map_page(struct protection_domain *dom,
  989. unsigned long bus_addr,
  990. unsigned long phys_addr,
  991. int prot,
  992. unsigned long page_size)
  993. {
  994. u64 __pte, *pte;
  995. int i, count;
  996. if (!(prot & IOMMU_PROT_MASK))
  997. return -EINVAL;
  998. bus_addr = PAGE_ALIGN(bus_addr);
  999. phys_addr = PAGE_ALIGN(phys_addr);
  1000. count = PAGE_SIZE_PTE_COUNT(page_size);
  1001. pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
  1002. for (i = 0; i < count; ++i)
  1003. if (IOMMU_PTE_PRESENT(pte[i]))
  1004. return -EBUSY;
  1005. if (page_size > PAGE_SIZE) {
  1006. __pte = PAGE_SIZE_PTE(phys_addr, page_size);
  1007. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
  1008. } else
  1009. __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1010. if (prot & IOMMU_PROT_IR)
  1011. __pte |= IOMMU_PTE_IR;
  1012. if (prot & IOMMU_PROT_IW)
  1013. __pte |= IOMMU_PTE_IW;
  1014. for (i = 0; i < count; ++i)
  1015. pte[i] = __pte;
  1016. update_domain(dom);
  1017. return 0;
  1018. }
  1019. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  1020. unsigned long bus_addr,
  1021. unsigned long page_size)
  1022. {
  1023. unsigned long long unmap_size, unmapped;
  1024. u64 *pte;
  1025. BUG_ON(!is_power_of_2(page_size));
  1026. unmapped = 0;
  1027. while (unmapped < page_size) {
  1028. pte = fetch_pte(dom, bus_addr);
  1029. if (!pte) {
  1030. /*
  1031. * No PTE for this address
  1032. * move forward in 4kb steps
  1033. */
  1034. unmap_size = PAGE_SIZE;
  1035. } else if (PM_PTE_LEVEL(*pte) == 0) {
  1036. /* 4kb PTE found for this address */
  1037. unmap_size = PAGE_SIZE;
  1038. *pte = 0ULL;
  1039. } else {
  1040. int count, i;
  1041. /* Large PTE found which maps this address */
  1042. unmap_size = PTE_PAGE_SIZE(*pte);
  1043. /* Only unmap from the first pte in the page */
  1044. if ((unmap_size - 1) & bus_addr)
  1045. break;
  1046. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  1047. for (i = 0; i < count; i++)
  1048. pte[i] = 0ULL;
  1049. }
  1050. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  1051. unmapped += unmap_size;
  1052. }
  1053. BUG_ON(unmapped && !is_power_of_2(unmapped));
  1054. return unmapped;
  1055. }
  1056. /*
  1057. * This function checks if a specific unity mapping entry is needed for
  1058. * this specific IOMMU.
  1059. */
  1060. static int iommu_for_unity_map(struct amd_iommu *iommu,
  1061. struct unity_map_entry *entry)
  1062. {
  1063. u16 bdf, i;
  1064. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  1065. bdf = amd_iommu_alias_table[i];
  1066. if (amd_iommu_rlookup_table[bdf] == iommu)
  1067. return 1;
  1068. }
  1069. return 0;
  1070. }
  1071. /*
  1072. * This function actually applies the mapping to the page table of the
  1073. * dma_ops domain.
  1074. */
  1075. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  1076. struct unity_map_entry *e)
  1077. {
  1078. u64 addr;
  1079. int ret;
  1080. for (addr = e->address_start; addr < e->address_end;
  1081. addr += PAGE_SIZE) {
  1082. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
  1083. PAGE_SIZE);
  1084. if (ret)
  1085. return ret;
  1086. /*
  1087. * if unity mapping is in aperture range mark the page
  1088. * as allocated in the aperture
  1089. */
  1090. if (addr < dma_dom->aperture_size)
  1091. __set_bit(addr >> PAGE_SHIFT,
  1092. dma_dom->aperture[0]->bitmap);
  1093. }
  1094. return 0;
  1095. }
  1096. /*
  1097. * Init the unity mappings for a specific IOMMU in the system
  1098. *
  1099. * Basically iterates over all unity mapping entries and applies them to
  1100. * the default domain DMA of that IOMMU if necessary.
  1101. */
  1102. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  1103. {
  1104. struct unity_map_entry *entry;
  1105. int ret;
  1106. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  1107. if (!iommu_for_unity_map(iommu, entry))
  1108. continue;
  1109. ret = dma_ops_unity_map(iommu->default_dom, entry);
  1110. if (ret)
  1111. return ret;
  1112. }
  1113. return 0;
  1114. }
  1115. /*
  1116. * Inits the unity mappings required for a specific device
  1117. */
  1118. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  1119. u16 devid)
  1120. {
  1121. struct unity_map_entry *e;
  1122. int ret;
  1123. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  1124. if (!(devid >= e->devid_start && devid <= e->devid_end))
  1125. continue;
  1126. ret = dma_ops_unity_map(dma_dom, e);
  1127. if (ret)
  1128. return ret;
  1129. }
  1130. return 0;
  1131. }
  1132. /****************************************************************************
  1133. *
  1134. * The next functions belong to the address allocator for the dma_ops
  1135. * interface functions. They work like the allocators in the other IOMMU
  1136. * drivers. Its basically a bitmap which marks the allocated pages in
  1137. * the aperture. Maybe it could be enhanced in the future to a more
  1138. * efficient allocator.
  1139. *
  1140. ****************************************************************************/
  1141. /*
  1142. * The address allocator core functions.
  1143. *
  1144. * called with domain->lock held
  1145. */
  1146. /*
  1147. * Used to reserve address ranges in the aperture (e.g. for exclusion
  1148. * ranges.
  1149. */
  1150. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  1151. unsigned long start_page,
  1152. unsigned int pages)
  1153. {
  1154. unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
  1155. if (start_page + pages > last_page)
  1156. pages = last_page - start_page;
  1157. for (i = start_page; i < start_page + pages; ++i) {
  1158. int index = i / APERTURE_RANGE_PAGES;
  1159. int page = i % APERTURE_RANGE_PAGES;
  1160. __set_bit(page, dom->aperture[index]->bitmap);
  1161. }
  1162. }
  1163. /*
  1164. * This function is used to add a new aperture range to an existing
  1165. * aperture in case of dma_ops domain allocation or address allocation
  1166. * failure.
  1167. */
  1168. static int alloc_new_range(struct dma_ops_domain *dma_dom,
  1169. bool populate, gfp_t gfp)
  1170. {
  1171. int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
  1172. struct amd_iommu *iommu;
  1173. unsigned long i, old_size;
  1174. #ifdef CONFIG_IOMMU_STRESS
  1175. populate = false;
  1176. #endif
  1177. if (index >= APERTURE_MAX_RANGES)
  1178. return -ENOMEM;
  1179. dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
  1180. if (!dma_dom->aperture[index])
  1181. return -ENOMEM;
  1182. dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
  1183. if (!dma_dom->aperture[index]->bitmap)
  1184. goto out_free;
  1185. dma_dom->aperture[index]->offset = dma_dom->aperture_size;
  1186. if (populate) {
  1187. unsigned long address = dma_dom->aperture_size;
  1188. int i, num_ptes = APERTURE_RANGE_PAGES / 512;
  1189. u64 *pte, *pte_page;
  1190. for (i = 0; i < num_ptes; ++i) {
  1191. pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
  1192. &pte_page, gfp);
  1193. if (!pte)
  1194. goto out_free;
  1195. dma_dom->aperture[index]->pte_pages[i] = pte_page;
  1196. address += APERTURE_RANGE_SIZE / 64;
  1197. }
  1198. }
  1199. old_size = dma_dom->aperture_size;
  1200. dma_dom->aperture_size += APERTURE_RANGE_SIZE;
  1201. /* Reserve address range used for MSI messages */
  1202. if (old_size < MSI_ADDR_BASE_LO &&
  1203. dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
  1204. unsigned long spage;
  1205. int pages;
  1206. pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
  1207. spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
  1208. dma_ops_reserve_addresses(dma_dom, spage, pages);
  1209. }
  1210. /* Initialize the exclusion range if necessary */
  1211. for_each_iommu(iommu) {
  1212. if (iommu->exclusion_start &&
  1213. iommu->exclusion_start >= dma_dom->aperture[index]->offset
  1214. && iommu->exclusion_start < dma_dom->aperture_size) {
  1215. unsigned long startpage;
  1216. int pages = iommu_num_pages(iommu->exclusion_start,
  1217. iommu->exclusion_length,
  1218. PAGE_SIZE);
  1219. startpage = iommu->exclusion_start >> PAGE_SHIFT;
  1220. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  1221. }
  1222. }
  1223. /*
  1224. * Check for areas already mapped as present in the new aperture
  1225. * range and mark those pages as reserved in the allocator. Such
  1226. * mappings may already exist as a result of requested unity
  1227. * mappings for devices.
  1228. */
  1229. for (i = dma_dom->aperture[index]->offset;
  1230. i < dma_dom->aperture_size;
  1231. i += PAGE_SIZE) {
  1232. u64 *pte = fetch_pte(&dma_dom->domain, i);
  1233. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  1234. continue;
  1235. dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
  1236. }
  1237. update_domain(&dma_dom->domain);
  1238. return 0;
  1239. out_free:
  1240. update_domain(&dma_dom->domain);
  1241. free_page((unsigned long)dma_dom->aperture[index]->bitmap);
  1242. kfree(dma_dom->aperture[index]);
  1243. dma_dom->aperture[index] = NULL;
  1244. return -ENOMEM;
  1245. }
  1246. static unsigned long dma_ops_area_alloc(struct device *dev,
  1247. struct dma_ops_domain *dom,
  1248. unsigned int pages,
  1249. unsigned long align_mask,
  1250. u64 dma_mask,
  1251. unsigned long start)
  1252. {
  1253. unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
  1254. int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
  1255. int i = start >> APERTURE_RANGE_SHIFT;
  1256. unsigned long boundary_size;
  1257. unsigned long address = -1;
  1258. unsigned long limit;
  1259. next_bit >>= PAGE_SHIFT;
  1260. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  1261. PAGE_SIZE) >> PAGE_SHIFT;
  1262. for (;i < max_index; ++i) {
  1263. unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
  1264. if (dom->aperture[i]->offset >= dma_mask)
  1265. break;
  1266. limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
  1267. dma_mask >> PAGE_SHIFT);
  1268. address = iommu_area_alloc(dom->aperture[i]->bitmap,
  1269. limit, next_bit, pages, 0,
  1270. boundary_size, align_mask);
  1271. if (address != -1) {
  1272. address = dom->aperture[i]->offset +
  1273. (address << PAGE_SHIFT);
  1274. dom->next_address = address + (pages << PAGE_SHIFT);
  1275. break;
  1276. }
  1277. next_bit = 0;
  1278. }
  1279. return address;
  1280. }
  1281. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  1282. struct dma_ops_domain *dom,
  1283. unsigned int pages,
  1284. unsigned long align_mask,
  1285. u64 dma_mask)
  1286. {
  1287. unsigned long address;
  1288. #ifdef CONFIG_IOMMU_STRESS
  1289. dom->next_address = 0;
  1290. dom->need_flush = true;
  1291. #endif
  1292. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1293. dma_mask, dom->next_address);
  1294. if (address == -1) {
  1295. dom->next_address = 0;
  1296. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1297. dma_mask, 0);
  1298. dom->need_flush = true;
  1299. }
  1300. if (unlikely(address == -1))
  1301. address = DMA_ERROR_CODE;
  1302. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  1303. return address;
  1304. }
  1305. /*
  1306. * The address free function.
  1307. *
  1308. * called with domain->lock held
  1309. */
  1310. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  1311. unsigned long address,
  1312. unsigned int pages)
  1313. {
  1314. unsigned i = address >> APERTURE_RANGE_SHIFT;
  1315. struct aperture_range *range = dom->aperture[i];
  1316. BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
  1317. #ifdef CONFIG_IOMMU_STRESS
  1318. if (i < 4)
  1319. return;
  1320. #endif
  1321. if (address >= dom->next_address)
  1322. dom->need_flush = true;
  1323. address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
  1324. bitmap_clear(range->bitmap, address, pages);
  1325. }
  1326. /****************************************************************************
  1327. *
  1328. * The next functions belong to the domain allocation. A domain is
  1329. * allocated for every IOMMU as the default domain. If device isolation
  1330. * is enabled, every device get its own domain. The most important thing
  1331. * about domains is the page table mapping the DMA address space they
  1332. * contain.
  1333. *
  1334. ****************************************************************************/
  1335. /*
  1336. * This function adds a protection domain to the global protection domain list
  1337. */
  1338. static void add_domain_to_list(struct protection_domain *domain)
  1339. {
  1340. unsigned long flags;
  1341. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1342. list_add(&domain->list, &amd_iommu_pd_list);
  1343. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1344. }
  1345. /*
  1346. * This function removes a protection domain to the global
  1347. * protection domain list
  1348. */
  1349. static void del_domain_from_list(struct protection_domain *domain)
  1350. {
  1351. unsigned long flags;
  1352. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1353. list_del(&domain->list);
  1354. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1355. }
  1356. static u16 domain_id_alloc(void)
  1357. {
  1358. unsigned long flags;
  1359. int id;
  1360. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1361. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1362. BUG_ON(id == 0);
  1363. if (id > 0 && id < MAX_DOMAIN_ID)
  1364. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1365. else
  1366. id = 0;
  1367. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1368. return id;
  1369. }
  1370. static void domain_id_free(int id)
  1371. {
  1372. unsigned long flags;
  1373. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1374. if (id > 0 && id < MAX_DOMAIN_ID)
  1375. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1376. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1377. }
  1378. static void free_pagetable(struct protection_domain *domain)
  1379. {
  1380. int i, j;
  1381. u64 *p1, *p2, *p3;
  1382. p1 = domain->pt_root;
  1383. if (!p1)
  1384. return;
  1385. for (i = 0; i < 512; ++i) {
  1386. if (!IOMMU_PTE_PRESENT(p1[i]))
  1387. continue;
  1388. p2 = IOMMU_PTE_PAGE(p1[i]);
  1389. for (j = 0; j < 512; ++j) {
  1390. if (!IOMMU_PTE_PRESENT(p2[j]))
  1391. continue;
  1392. p3 = IOMMU_PTE_PAGE(p2[j]);
  1393. free_page((unsigned long)p3);
  1394. }
  1395. free_page((unsigned long)p2);
  1396. }
  1397. free_page((unsigned long)p1);
  1398. domain->pt_root = NULL;
  1399. }
  1400. static void free_gcr3_tbl_level1(u64 *tbl)
  1401. {
  1402. u64 *ptr;
  1403. int i;
  1404. for (i = 0; i < 512; ++i) {
  1405. if (!(tbl[i] & GCR3_VALID))
  1406. continue;
  1407. ptr = __va(tbl[i] & PAGE_MASK);
  1408. free_page((unsigned long)ptr);
  1409. }
  1410. }
  1411. static void free_gcr3_tbl_level2(u64 *tbl)
  1412. {
  1413. u64 *ptr;
  1414. int i;
  1415. for (i = 0; i < 512; ++i) {
  1416. if (!(tbl[i] & GCR3_VALID))
  1417. continue;
  1418. ptr = __va(tbl[i] & PAGE_MASK);
  1419. free_gcr3_tbl_level1(ptr);
  1420. }
  1421. }
  1422. static void free_gcr3_table(struct protection_domain *domain)
  1423. {
  1424. if (domain->glx == 2)
  1425. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1426. else if (domain->glx == 1)
  1427. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1428. else if (domain->glx != 0)
  1429. BUG();
  1430. free_page((unsigned long)domain->gcr3_tbl);
  1431. }
  1432. /*
  1433. * Free a domain, only used if something went wrong in the
  1434. * allocation path and we need to free an already allocated page table
  1435. */
  1436. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1437. {
  1438. int i;
  1439. if (!dom)
  1440. return;
  1441. del_domain_from_list(&dom->domain);
  1442. free_pagetable(&dom->domain);
  1443. for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
  1444. if (!dom->aperture[i])
  1445. continue;
  1446. free_page((unsigned long)dom->aperture[i]->bitmap);
  1447. kfree(dom->aperture[i]);
  1448. }
  1449. kfree(dom);
  1450. }
  1451. /*
  1452. * Allocates a new protection domain usable for the dma_ops functions.
  1453. * It also initializes the page table and the address allocator data
  1454. * structures required for the dma_ops interface
  1455. */
  1456. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1457. {
  1458. struct dma_ops_domain *dma_dom;
  1459. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1460. if (!dma_dom)
  1461. return NULL;
  1462. spin_lock_init(&dma_dom->domain.lock);
  1463. dma_dom->domain.id = domain_id_alloc();
  1464. if (dma_dom->domain.id == 0)
  1465. goto free_dma_dom;
  1466. INIT_LIST_HEAD(&dma_dom->domain.dev_list);
  1467. dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
  1468. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1469. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1470. dma_dom->domain.priv = dma_dom;
  1471. if (!dma_dom->domain.pt_root)
  1472. goto free_dma_dom;
  1473. dma_dom->need_flush = false;
  1474. dma_dom->target_dev = 0xffff;
  1475. add_domain_to_list(&dma_dom->domain);
  1476. if (alloc_new_range(dma_dom, true, GFP_KERNEL))
  1477. goto free_dma_dom;
  1478. /*
  1479. * mark the first page as allocated so we never return 0 as
  1480. * a valid dma-address. So we can use 0 as error value
  1481. */
  1482. dma_dom->aperture[0]->bitmap[0] = 1;
  1483. dma_dom->next_address = 0;
  1484. return dma_dom;
  1485. free_dma_dom:
  1486. dma_ops_domain_free(dma_dom);
  1487. return NULL;
  1488. }
  1489. /*
  1490. * little helper function to check whether a given protection domain is a
  1491. * dma_ops domain
  1492. */
  1493. static bool dma_ops_domain(struct protection_domain *domain)
  1494. {
  1495. return domain->flags & PD_DMA_OPS_MASK;
  1496. }
  1497. static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
  1498. {
  1499. u64 pte_root = 0;
  1500. u64 flags = 0;
  1501. if (domain->mode != PAGE_MODE_NONE)
  1502. pte_root = virt_to_phys(domain->pt_root);
  1503. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1504. << DEV_ENTRY_MODE_SHIFT;
  1505. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  1506. flags = amd_iommu_dev_table[devid].data[1];
  1507. if (ats)
  1508. flags |= DTE_FLAG_IOTLB;
  1509. if (domain->flags & PD_IOMMUV2_MASK) {
  1510. u64 gcr3 = __pa(domain->gcr3_tbl);
  1511. u64 glx = domain->glx;
  1512. u64 tmp;
  1513. pte_root |= DTE_FLAG_GV;
  1514. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1515. /* First mask out possible old values for GCR3 table */
  1516. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1517. flags &= ~tmp;
  1518. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1519. flags &= ~tmp;
  1520. /* Encode GCR3 table into DTE */
  1521. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1522. pte_root |= tmp;
  1523. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1524. flags |= tmp;
  1525. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1526. flags |= tmp;
  1527. }
  1528. flags &= ~(0xffffUL);
  1529. flags |= domain->id;
  1530. amd_iommu_dev_table[devid].data[1] = flags;
  1531. amd_iommu_dev_table[devid].data[0] = pte_root;
  1532. }
  1533. static void clear_dte_entry(u16 devid)
  1534. {
  1535. /* remove entry from the device table seen by the hardware */
  1536. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  1537. amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
  1538. amd_iommu_apply_erratum_63(devid);
  1539. }
  1540. static void do_attach(struct iommu_dev_data *dev_data,
  1541. struct protection_domain *domain)
  1542. {
  1543. struct amd_iommu *iommu;
  1544. bool ats;
  1545. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1546. ats = dev_data->ats.enabled;
  1547. /* Update data structures */
  1548. dev_data->domain = domain;
  1549. list_add(&dev_data->list, &domain->dev_list);
  1550. set_dte_entry(dev_data->devid, domain, ats);
  1551. /* Do reference counting */
  1552. domain->dev_iommu[iommu->index] += 1;
  1553. domain->dev_cnt += 1;
  1554. /* Flush the DTE entry */
  1555. device_flush_dte(dev_data);
  1556. }
  1557. static void do_detach(struct iommu_dev_data *dev_data)
  1558. {
  1559. struct amd_iommu *iommu;
  1560. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1561. /* decrease reference counters */
  1562. dev_data->domain->dev_iommu[iommu->index] -= 1;
  1563. dev_data->domain->dev_cnt -= 1;
  1564. /* Update data structures */
  1565. dev_data->domain = NULL;
  1566. list_del(&dev_data->list);
  1567. clear_dte_entry(dev_data->devid);
  1568. /* Flush the DTE entry */
  1569. device_flush_dte(dev_data);
  1570. }
  1571. /*
  1572. * If a device is not yet associated with a domain, this function does
  1573. * assigns it visible for the hardware
  1574. */
  1575. static int __attach_device(struct iommu_dev_data *dev_data,
  1576. struct protection_domain *domain)
  1577. {
  1578. int ret;
  1579. /* lock domain */
  1580. spin_lock(&domain->lock);
  1581. if (dev_data->alias_data != NULL) {
  1582. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1583. /* Some sanity checks */
  1584. ret = -EBUSY;
  1585. if (alias_data->domain != NULL &&
  1586. alias_data->domain != domain)
  1587. goto out_unlock;
  1588. if (dev_data->domain != NULL &&
  1589. dev_data->domain != domain)
  1590. goto out_unlock;
  1591. /* Do real assignment */
  1592. if (alias_data->domain == NULL)
  1593. do_attach(alias_data, domain);
  1594. atomic_inc(&alias_data->bind);
  1595. }
  1596. if (dev_data->domain == NULL)
  1597. do_attach(dev_data, domain);
  1598. atomic_inc(&dev_data->bind);
  1599. ret = 0;
  1600. out_unlock:
  1601. /* ready */
  1602. spin_unlock(&domain->lock);
  1603. return ret;
  1604. }
  1605. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1606. {
  1607. pci_disable_ats(pdev);
  1608. pci_disable_pri(pdev);
  1609. pci_disable_pasid(pdev);
  1610. }
  1611. /* FIXME: Change generic reset-function to do the same */
  1612. static int pri_reset_while_enabled(struct pci_dev *pdev)
  1613. {
  1614. u16 control;
  1615. int pos;
  1616. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1617. if (!pos)
  1618. return -EINVAL;
  1619. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  1620. control |= PCI_PRI_CTRL_RESET;
  1621. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  1622. return 0;
  1623. }
  1624. static int pdev_iommuv2_enable(struct pci_dev *pdev)
  1625. {
  1626. bool reset_enable;
  1627. int reqs, ret;
  1628. /* FIXME: Hardcode number of outstanding requests for now */
  1629. reqs = 32;
  1630. if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
  1631. reqs = 1;
  1632. reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
  1633. /* Only allow access to user-accessible pages */
  1634. ret = pci_enable_pasid(pdev, 0);
  1635. if (ret)
  1636. goto out_err;
  1637. /* First reset the PRI state of the device */
  1638. ret = pci_reset_pri(pdev);
  1639. if (ret)
  1640. goto out_err;
  1641. /* Enable PRI */
  1642. ret = pci_enable_pri(pdev, reqs);
  1643. if (ret)
  1644. goto out_err;
  1645. if (reset_enable) {
  1646. ret = pri_reset_while_enabled(pdev);
  1647. if (ret)
  1648. goto out_err;
  1649. }
  1650. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1651. if (ret)
  1652. goto out_err;
  1653. return 0;
  1654. out_err:
  1655. pci_disable_pri(pdev);
  1656. pci_disable_pasid(pdev);
  1657. return ret;
  1658. }
  1659. /* FIXME: Move this to PCI code */
  1660. #define PCI_PRI_TLP_OFF (1 << 15)
  1661. bool pci_pri_tlp_required(struct pci_dev *pdev)
  1662. {
  1663. u16 status;
  1664. int pos;
  1665. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1666. if (!pos)
  1667. return false;
  1668. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  1669. return (status & PCI_PRI_TLP_OFF) ? true : false;
  1670. }
  1671. /*
  1672. * If a device is not yet associated with a domain, this function does
  1673. * assigns it visible for the hardware
  1674. */
  1675. static int attach_device(struct device *dev,
  1676. struct protection_domain *domain)
  1677. {
  1678. struct pci_dev *pdev = to_pci_dev(dev);
  1679. struct iommu_dev_data *dev_data;
  1680. unsigned long flags;
  1681. int ret;
  1682. dev_data = get_dev_data(dev);
  1683. if (domain->flags & PD_IOMMUV2_MASK) {
  1684. if (!dev_data->iommu_v2 || !dev_data->passthrough)
  1685. return -EINVAL;
  1686. if (pdev_iommuv2_enable(pdev) != 0)
  1687. return -EINVAL;
  1688. dev_data->ats.enabled = true;
  1689. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1690. dev_data->pri_tlp = pci_pri_tlp_required(pdev);
  1691. } else if (amd_iommu_iotlb_sup &&
  1692. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1693. dev_data->ats.enabled = true;
  1694. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1695. }
  1696. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1697. ret = __attach_device(dev_data, domain);
  1698. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1699. /*
  1700. * We might boot into a crash-kernel here. The crashed kernel
  1701. * left the caches in the IOMMU dirty. So we have to flush
  1702. * here to evict all dirty stuff.
  1703. */
  1704. domain_flush_tlb_pde(domain);
  1705. return ret;
  1706. }
  1707. /*
  1708. * Removes a device from a protection domain (unlocked)
  1709. */
  1710. static void __detach_device(struct iommu_dev_data *dev_data)
  1711. {
  1712. struct protection_domain *domain;
  1713. unsigned long flags;
  1714. BUG_ON(!dev_data->domain);
  1715. domain = dev_data->domain;
  1716. spin_lock_irqsave(&domain->lock, flags);
  1717. if (dev_data->alias_data != NULL) {
  1718. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1719. if (atomic_dec_and_test(&alias_data->bind))
  1720. do_detach(alias_data);
  1721. }
  1722. if (atomic_dec_and_test(&dev_data->bind))
  1723. do_detach(dev_data);
  1724. spin_unlock_irqrestore(&domain->lock, flags);
  1725. /*
  1726. * If we run in passthrough mode the device must be assigned to the
  1727. * passthrough domain if it is detached from any other domain.
  1728. * Make sure we can deassign from the pt_domain itself.
  1729. */
  1730. if (dev_data->passthrough &&
  1731. (dev_data->domain == NULL && domain != pt_domain))
  1732. __attach_device(dev_data, pt_domain);
  1733. }
  1734. /*
  1735. * Removes a device from a protection domain (with devtable_lock held)
  1736. */
  1737. static void detach_device(struct device *dev)
  1738. {
  1739. struct protection_domain *domain;
  1740. struct iommu_dev_data *dev_data;
  1741. unsigned long flags;
  1742. dev_data = get_dev_data(dev);
  1743. domain = dev_data->domain;
  1744. /* lock device table */
  1745. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1746. __detach_device(dev_data);
  1747. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1748. if (domain->flags & PD_IOMMUV2_MASK)
  1749. pdev_iommuv2_disable(to_pci_dev(dev));
  1750. else if (dev_data->ats.enabled)
  1751. pci_disable_ats(to_pci_dev(dev));
  1752. dev_data->ats.enabled = false;
  1753. }
  1754. /*
  1755. * Find out the protection domain structure for a given PCI device. This
  1756. * will give us the pointer to the page table root for example.
  1757. */
  1758. static struct protection_domain *domain_for_device(struct device *dev)
  1759. {
  1760. struct iommu_dev_data *dev_data;
  1761. struct protection_domain *dom = NULL;
  1762. unsigned long flags;
  1763. dev_data = get_dev_data(dev);
  1764. if (dev_data->domain)
  1765. return dev_data->domain;
  1766. if (dev_data->alias_data != NULL) {
  1767. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1768. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1769. if (alias_data->domain != NULL) {
  1770. __attach_device(dev_data, alias_data->domain);
  1771. dom = alias_data->domain;
  1772. }
  1773. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1774. }
  1775. return dom;
  1776. }
  1777. static int device_change_notifier(struct notifier_block *nb,
  1778. unsigned long action, void *data)
  1779. {
  1780. struct dma_ops_domain *dma_domain;
  1781. struct protection_domain *domain;
  1782. struct iommu_dev_data *dev_data;
  1783. struct device *dev = data;
  1784. struct amd_iommu *iommu;
  1785. unsigned long flags;
  1786. u16 devid;
  1787. if (!check_device(dev))
  1788. return 0;
  1789. devid = get_device_id(dev);
  1790. iommu = amd_iommu_rlookup_table[devid];
  1791. dev_data = get_dev_data(dev);
  1792. switch (action) {
  1793. case BUS_NOTIFY_UNBOUND_DRIVER:
  1794. domain = domain_for_device(dev);
  1795. if (!domain)
  1796. goto out;
  1797. if (dev_data->passthrough)
  1798. break;
  1799. detach_device(dev);
  1800. break;
  1801. case BUS_NOTIFY_ADD_DEVICE:
  1802. iommu_init_device(dev);
  1803. /*
  1804. * dev_data is still NULL and
  1805. * got initialized in iommu_init_device
  1806. */
  1807. dev_data = get_dev_data(dev);
  1808. if (iommu_pass_through || dev_data->iommu_v2) {
  1809. dev_data->passthrough = true;
  1810. attach_device(dev, pt_domain);
  1811. break;
  1812. }
  1813. domain = domain_for_device(dev);
  1814. /* allocate a protection domain if a device is added */
  1815. dma_domain = find_protection_domain(devid);
  1816. if (!dma_domain) {
  1817. dma_domain = dma_ops_domain_alloc();
  1818. if (!dma_domain)
  1819. goto out;
  1820. dma_domain->target_dev = devid;
  1821. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  1822. list_add_tail(&dma_domain->list, &iommu_pd_list);
  1823. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  1824. }
  1825. dev->archdata.dma_ops = &amd_iommu_dma_ops;
  1826. break;
  1827. case BUS_NOTIFY_DEL_DEVICE:
  1828. iommu_uninit_device(dev);
  1829. default:
  1830. goto out;
  1831. }
  1832. iommu_completion_wait(iommu);
  1833. out:
  1834. return 0;
  1835. }
  1836. static struct notifier_block device_nb = {
  1837. .notifier_call = device_change_notifier,
  1838. };
  1839. void amd_iommu_init_notifier(void)
  1840. {
  1841. bus_register_notifier(&pci_bus_type, &device_nb);
  1842. }
  1843. /*****************************************************************************
  1844. *
  1845. * The next functions belong to the dma_ops mapping/unmapping code.
  1846. *
  1847. *****************************************************************************/
  1848. /*
  1849. * In the dma_ops path we only have the struct device. This function
  1850. * finds the corresponding IOMMU, the protection domain and the
  1851. * requestor id for a given device.
  1852. * If the device is not yet associated with a domain this is also done
  1853. * in this function.
  1854. */
  1855. static struct protection_domain *get_domain(struct device *dev)
  1856. {
  1857. struct protection_domain *domain;
  1858. struct dma_ops_domain *dma_dom;
  1859. u16 devid = get_device_id(dev);
  1860. if (!check_device(dev))
  1861. return ERR_PTR(-EINVAL);
  1862. domain = domain_for_device(dev);
  1863. if (domain != NULL && !dma_ops_domain(domain))
  1864. return ERR_PTR(-EBUSY);
  1865. if (domain != NULL)
  1866. return domain;
  1867. /* Device not bount yet - bind it */
  1868. dma_dom = find_protection_domain(devid);
  1869. if (!dma_dom)
  1870. dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
  1871. attach_device(dev, &dma_dom->domain);
  1872. DUMP_printk("Using protection domain %d for device %s\n",
  1873. dma_dom->domain.id, dev_name(dev));
  1874. return &dma_dom->domain;
  1875. }
  1876. static void update_device_table(struct protection_domain *domain)
  1877. {
  1878. struct iommu_dev_data *dev_data;
  1879. list_for_each_entry(dev_data, &domain->dev_list, list)
  1880. set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
  1881. }
  1882. static void update_domain(struct protection_domain *domain)
  1883. {
  1884. if (!domain->updated)
  1885. return;
  1886. update_device_table(domain);
  1887. domain_flush_devices(domain);
  1888. domain_flush_tlb_pde(domain);
  1889. domain->updated = false;
  1890. }
  1891. /*
  1892. * This function fetches the PTE for a given address in the aperture
  1893. */
  1894. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  1895. unsigned long address)
  1896. {
  1897. struct aperture_range *aperture;
  1898. u64 *pte, *pte_page;
  1899. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1900. if (!aperture)
  1901. return NULL;
  1902. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1903. if (!pte) {
  1904. pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
  1905. GFP_ATOMIC);
  1906. aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
  1907. } else
  1908. pte += PM_LEVEL_INDEX(0, address);
  1909. update_domain(&dom->domain);
  1910. return pte;
  1911. }
  1912. /*
  1913. * This is the generic map function. It maps one 4kb page at paddr to
  1914. * the given address in the DMA address space for the domain.
  1915. */
  1916. static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
  1917. unsigned long address,
  1918. phys_addr_t paddr,
  1919. int direction)
  1920. {
  1921. u64 *pte, __pte;
  1922. WARN_ON(address > dom->aperture_size);
  1923. paddr &= PAGE_MASK;
  1924. pte = dma_ops_get_pte(dom, address);
  1925. if (!pte)
  1926. return DMA_ERROR_CODE;
  1927. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1928. if (direction == DMA_TO_DEVICE)
  1929. __pte |= IOMMU_PTE_IR;
  1930. else if (direction == DMA_FROM_DEVICE)
  1931. __pte |= IOMMU_PTE_IW;
  1932. else if (direction == DMA_BIDIRECTIONAL)
  1933. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  1934. WARN_ON(*pte);
  1935. *pte = __pte;
  1936. return (dma_addr_t)address;
  1937. }
  1938. /*
  1939. * The generic unmapping function for on page in the DMA address space.
  1940. */
  1941. static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
  1942. unsigned long address)
  1943. {
  1944. struct aperture_range *aperture;
  1945. u64 *pte;
  1946. if (address >= dom->aperture_size)
  1947. return;
  1948. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1949. if (!aperture)
  1950. return;
  1951. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1952. if (!pte)
  1953. return;
  1954. pte += PM_LEVEL_INDEX(0, address);
  1955. WARN_ON(!*pte);
  1956. *pte = 0ULL;
  1957. }
  1958. /*
  1959. * This function contains common code for mapping of a physically
  1960. * contiguous memory region into DMA address space. It is used by all
  1961. * mapping functions provided with this IOMMU driver.
  1962. * Must be called with the domain lock held.
  1963. */
  1964. static dma_addr_t __map_single(struct device *dev,
  1965. struct dma_ops_domain *dma_dom,
  1966. phys_addr_t paddr,
  1967. size_t size,
  1968. int dir,
  1969. bool align,
  1970. u64 dma_mask)
  1971. {
  1972. dma_addr_t offset = paddr & ~PAGE_MASK;
  1973. dma_addr_t address, start, ret;
  1974. unsigned int pages;
  1975. unsigned long align_mask = 0;
  1976. int i;
  1977. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1978. paddr &= PAGE_MASK;
  1979. INC_STATS_COUNTER(total_map_requests);
  1980. if (pages > 1)
  1981. INC_STATS_COUNTER(cross_page);
  1982. if (align)
  1983. align_mask = (1UL << get_order(size)) - 1;
  1984. retry:
  1985. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1986. dma_mask);
  1987. if (unlikely(address == DMA_ERROR_CODE)) {
  1988. /*
  1989. * setting next_address here will let the address
  1990. * allocator only scan the new allocated range in the
  1991. * first run. This is a small optimization.
  1992. */
  1993. dma_dom->next_address = dma_dom->aperture_size;
  1994. if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
  1995. goto out;
  1996. /*
  1997. * aperture was successfully enlarged by 128 MB, try
  1998. * allocation again
  1999. */
  2000. goto retry;
  2001. }
  2002. start = address;
  2003. for (i = 0; i < pages; ++i) {
  2004. ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
  2005. if (ret == DMA_ERROR_CODE)
  2006. goto out_unmap;
  2007. paddr += PAGE_SIZE;
  2008. start += PAGE_SIZE;
  2009. }
  2010. address += offset;
  2011. ADD_STATS_COUNTER(alloced_io_mem, size);
  2012. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  2013. domain_flush_tlb(&dma_dom->domain);
  2014. dma_dom->need_flush = false;
  2015. } else if (unlikely(amd_iommu_np_cache))
  2016. domain_flush_pages(&dma_dom->domain, address, size);
  2017. out:
  2018. return address;
  2019. out_unmap:
  2020. for (--i; i >= 0; --i) {
  2021. start -= PAGE_SIZE;
  2022. dma_ops_domain_unmap(dma_dom, start);
  2023. }
  2024. dma_ops_free_addresses(dma_dom, address, pages);
  2025. return DMA_ERROR_CODE;
  2026. }
  2027. /*
  2028. * Does the reverse of the __map_single function. Must be called with
  2029. * the domain lock held too
  2030. */
  2031. static void __unmap_single(struct dma_ops_domain *dma_dom,
  2032. dma_addr_t dma_addr,
  2033. size_t size,
  2034. int dir)
  2035. {
  2036. dma_addr_t flush_addr;
  2037. dma_addr_t i, start;
  2038. unsigned int pages;
  2039. if ((dma_addr == DMA_ERROR_CODE) ||
  2040. (dma_addr + size > dma_dom->aperture_size))
  2041. return;
  2042. flush_addr = dma_addr;
  2043. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  2044. dma_addr &= PAGE_MASK;
  2045. start = dma_addr;
  2046. for (i = 0; i < pages; ++i) {
  2047. dma_ops_domain_unmap(dma_dom, start);
  2048. start += PAGE_SIZE;
  2049. }
  2050. SUB_STATS_COUNTER(alloced_io_mem, size);
  2051. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  2052. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  2053. domain_flush_pages(&dma_dom->domain, flush_addr, size);
  2054. dma_dom->need_flush = false;
  2055. }
  2056. }
  2057. /*
  2058. * The exported map_single function for dma_ops.
  2059. */
  2060. static dma_addr_t map_page(struct device *dev, struct page *page,
  2061. unsigned long offset, size_t size,
  2062. enum dma_data_direction dir,
  2063. struct dma_attrs *attrs)
  2064. {
  2065. unsigned long flags;
  2066. struct protection_domain *domain;
  2067. dma_addr_t addr;
  2068. u64 dma_mask;
  2069. phys_addr_t paddr = page_to_phys(page) + offset;
  2070. INC_STATS_COUNTER(cnt_map_single);
  2071. domain = get_domain(dev);
  2072. if (PTR_ERR(domain) == -EINVAL)
  2073. return (dma_addr_t)paddr;
  2074. else if (IS_ERR(domain))
  2075. return DMA_ERROR_CODE;
  2076. dma_mask = *dev->dma_mask;
  2077. spin_lock_irqsave(&domain->lock, flags);
  2078. addr = __map_single(dev, domain->priv, paddr, size, dir, false,
  2079. dma_mask);
  2080. if (addr == DMA_ERROR_CODE)
  2081. goto out;
  2082. domain_flush_complete(domain);
  2083. out:
  2084. spin_unlock_irqrestore(&domain->lock, flags);
  2085. return addr;
  2086. }
  2087. /*
  2088. * The exported unmap_single function for dma_ops.
  2089. */
  2090. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  2091. enum dma_data_direction dir, struct dma_attrs *attrs)
  2092. {
  2093. unsigned long flags;
  2094. struct protection_domain *domain;
  2095. INC_STATS_COUNTER(cnt_unmap_single);
  2096. domain = get_domain(dev);
  2097. if (IS_ERR(domain))
  2098. return;
  2099. spin_lock_irqsave(&domain->lock, flags);
  2100. __unmap_single(domain->priv, dma_addr, size, dir);
  2101. domain_flush_complete(domain);
  2102. spin_unlock_irqrestore(&domain->lock, flags);
  2103. }
  2104. /*
  2105. * This is a special map_sg function which is used if we should map a
  2106. * device which is not handled by an AMD IOMMU in the system.
  2107. */
  2108. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  2109. int nelems, int dir)
  2110. {
  2111. struct scatterlist *s;
  2112. int i;
  2113. for_each_sg(sglist, s, nelems, i) {
  2114. s->dma_address = (dma_addr_t)sg_phys(s);
  2115. s->dma_length = s->length;
  2116. }
  2117. return nelems;
  2118. }
  2119. /*
  2120. * The exported map_sg function for dma_ops (handles scatter-gather
  2121. * lists).
  2122. */
  2123. static int map_sg(struct device *dev, struct scatterlist *sglist,
  2124. int nelems, enum dma_data_direction dir,
  2125. struct dma_attrs *attrs)
  2126. {
  2127. unsigned long flags;
  2128. struct protection_domain *domain;
  2129. int i;
  2130. struct scatterlist *s;
  2131. phys_addr_t paddr;
  2132. int mapped_elems = 0;
  2133. u64 dma_mask;
  2134. INC_STATS_COUNTER(cnt_map_sg);
  2135. domain = get_domain(dev);
  2136. if (PTR_ERR(domain) == -EINVAL)
  2137. return map_sg_no_iommu(dev, sglist, nelems, dir);
  2138. else if (IS_ERR(domain))
  2139. return 0;
  2140. dma_mask = *dev->dma_mask;
  2141. spin_lock_irqsave(&domain->lock, flags);
  2142. for_each_sg(sglist, s, nelems, i) {
  2143. paddr = sg_phys(s);
  2144. s->dma_address = __map_single(dev, domain->priv,
  2145. paddr, s->length, dir, false,
  2146. dma_mask);
  2147. if (s->dma_address) {
  2148. s->dma_length = s->length;
  2149. mapped_elems++;
  2150. } else
  2151. goto unmap;
  2152. }
  2153. domain_flush_complete(domain);
  2154. out:
  2155. spin_unlock_irqrestore(&domain->lock, flags);
  2156. return mapped_elems;
  2157. unmap:
  2158. for_each_sg(sglist, s, mapped_elems, i) {
  2159. if (s->dma_address)
  2160. __unmap_single(domain->priv, s->dma_address,
  2161. s->dma_length, dir);
  2162. s->dma_address = s->dma_length = 0;
  2163. }
  2164. mapped_elems = 0;
  2165. goto out;
  2166. }
  2167. /*
  2168. * The exported map_sg function for dma_ops (handles scatter-gather
  2169. * lists).
  2170. */
  2171. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  2172. int nelems, enum dma_data_direction dir,
  2173. struct dma_attrs *attrs)
  2174. {
  2175. unsigned long flags;
  2176. struct protection_domain *domain;
  2177. struct scatterlist *s;
  2178. int i;
  2179. INC_STATS_COUNTER(cnt_unmap_sg);
  2180. domain = get_domain(dev);
  2181. if (IS_ERR(domain))
  2182. return;
  2183. spin_lock_irqsave(&domain->lock, flags);
  2184. for_each_sg(sglist, s, nelems, i) {
  2185. __unmap_single(domain->priv, s->dma_address,
  2186. s->dma_length, dir);
  2187. s->dma_address = s->dma_length = 0;
  2188. }
  2189. domain_flush_complete(domain);
  2190. spin_unlock_irqrestore(&domain->lock, flags);
  2191. }
  2192. /*
  2193. * The exported alloc_coherent function for dma_ops.
  2194. */
  2195. static void *alloc_coherent(struct device *dev, size_t size,
  2196. dma_addr_t *dma_addr, gfp_t flag,
  2197. struct dma_attrs *attrs)
  2198. {
  2199. unsigned long flags;
  2200. void *virt_addr;
  2201. struct protection_domain *domain;
  2202. phys_addr_t paddr;
  2203. u64 dma_mask = dev->coherent_dma_mask;
  2204. INC_STATS_COUNTER(cnt_alloc_coherent);
  2205. domain = get_domain(dev);
  2206. if (PTR_ERR(domain) == -EINVAL) {
  2207. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  2208. *dma_addr = __pa(virt_addr);
  2209. return virt_addr;
  2210. } else if (IS_ERR(domain))
  2211. return NULL;
  2212. dma_mask = dev->coherent_dma_mask;
  2213. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  2214. flag |= __GFP_ZERO;
  2215. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  2216. if (!virt_addr)
  2217. return NULL;
  2218. paddr = virt_to_phys(virt_addr);
  2219. if (!dma_mask)
  2220. dma_mask = *dev->dma_mask;
  2221. spin_lock_irqsave(&domain->lock, flags);
  2222. *dma_addr = __map_single(dev, domain->priv, paddr,
  2223. size, DMA_BIDIRECTIONAL, true, dma_mask);
  2224. if (*dma_addr == DMA_ERROR_CODE) {
  2225. spin_unlock_irqrestore(&domain->lock, flags);
  2226. goto out_free;
  2227. }
  2228. domain_flush_complete(domain);
  2229. spin_unlock_irqrestore(&domain->lock, flags);
  2230. return virt_addr;
  2231. out_free:
  2232. free_pages((unsigned long)virt_addr, get_order(size));
  2233. return NULL;
  2234. }
  2235. /*
  2236. * The exported free_coherent function for dma_ops.
  2237. */
  2238. static void free_coherent(struct device *dev, size_t size,
  2239. void *virt_addr, dma_addr_t dma_addr,
  2240. struct dma_attrs *attrs)
  2241. {
  2242. unsigned long flags;
  2243. struct protection_domain *domain;
  2244. INC_STATS_COUNTER(cnt_free_coherent);
  2245. domain = get_domain(dev);
  2246. if (IS_ERR(domain))
  2247. goto free_mem;
  2248. spin_lock_irqsave(&domain->lock, flags);
  2249. __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  2250. domain_flush_complete(domain);
  2251. spin_unlock_irqrestore(&domain->lock, flags);
  2252. free_mem:
  2253. free_pages((unsigned long)virt_addr, get_order(size));
  2254. }
  2255. /*
  2256. * This function is called by the DMA layer to find out if we can handle a
  2257. * particular device. It is part of the dma_ops.
  2258. */
  2259. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  2260. {
  2261. return check_device(dev);
  2262. }
  2263. /*
  2264. * The function for pre-allocating protection domains.
  2265. *
  2266. * If the driver core informs the DMA layer if a driver grabs a device
  2267. * we don't need to preallocate the protection domains anymore.
  2268. * For now we have to.
  2269. */
  2270. static void __init prealloc_protection_domains(void)
  2271. {
  2272. struct iommu_dev_data *dev_data;
  2273. struct dma_ops_domain *dma_dom;
  2274. struct pci_dev *dev = NULL;
  2275. u16 devid;
  2276. for_each_pci_dev(dev) {
  2277. /* Do we handle this device? */
  2278. if (!check_device(&dev->dev))
  2279. continue;
  2280. dev_data = get_dev_data(&dev->dev);
  2281. if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
  2282. /* Make sure passthrough domain is allocated */
  2283. alloc_passthrough_domain();
  2284. dev_data->passthrough = true;
  2285. attach_device(&dev->dev, pt_domain);
  2286. pr_info("AMD-Vi: Using passthough domain for device %s\n",
  2287. dev_name(&dev->dev));
  2288. }
  2289. /* Is there already any domain for it? */
  2290. if (domain_for_device(&dev->dev))
  2291. continue;
  2292. devid = get_device_id(&dev->dev);
  2293. dma_dom = dma_ops_domain_alloc();
  2294. if (!dma_dom)
  2295. continue;
  2296. init_unity_mappings_for_device(dma_dom, devid);
  2297. dma_dom->target_dev = devid;
  2298. attach_device(&dev->dev, &dma_dom->domain);
  2299. list_add_tail(&dma_dom->list, &iommu_pd_list);
  2300. }
  2301. }
  2302. static struct dma_map_ops amd_iommu_dma_ops = {
  2303. .alloc = alloc_coherent,
  2304. .free = free_coherent,
  2305. .map_page = map_page,
  2306. .unmap_page = unmap_page,
  2307. .map_sg = map_sg,
  2308. .unmap_sg = unmap_sg,
  2309. .dma_supported = amd_iommu_dma_supported,
  2310. };
  2311. static unsigned device_dma_ops_init(void)
  2312. {
  2313. struct iommu_dev_data *dev_data;
  2314. struct pci_dev *pdev = NULL;
  2315. unsigned unhandled = 0;
  2316. for_each_pci_dev(pdev) {
  2317. if (!check_device(&pdev->dev)) {
  2318. iommu_ignore_device(&pdev->dev);
  2319. unhandled += 1;
  2320. continue;
  2321. }
  2322. dev_data = get_dev_data(&pdev->dev);
  2323. if (!dev_data->passthrough)
  2324. pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
  2325. else
  2326. pdev->dev.archdata.dma_ops = &nommu_dma_ops;
  2327. }
  2328. return unhandled;
  2329. }
  2330. /*
  2331. * The function which clues the AMD IOMMU driver into dma_ops.
  2332. */
  2333. void __init amd_iommu_init_api(void)
  2334. {
  2335. bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
  2336. }
  2337. int __init amd_iommu_init_dma_ops(void)
  2338. {
  2339. struct amd_iommu *iommu;
  2340. int ret, unhandled;
  2341. /*
  2342. * first allocate a default protection domain for every IOMMU we
  2343. * found in the system. Devices not assigned to any other
  2344. * protection domain will be assigned to the default one.
  2345. */
  2346. for_each_iommu(iommu) {
  2347. iommu->default_dom = dma_ops_domain_alloc();
  2348. if (iommu->default_dom == NULL)
  2349. return -ENOMEM;
  2350. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  2351. ret = iommu_init_unity_mappings(iommu);
  2352. if (ret)
  2353. goto free_domains;
  2354. }
  2355. /*
  2356. * Pre-allocate the protection domains for each device.
  2357. */
  2358. prealloc_protection_domains();
  2359. iommu_detected = 1;
  2360. swiotlb = 0;
  2361. /* Make the driver finally visible to the drivers */
  2362. unhandled = device_dma_ops_init();
  2363. if (unhandled && max_pfn > MAX_DMA32_PFN) {
  2364. /* There are unhandled devices - initialize swiotlb for them */
  2365. swiotlb = 1;
  2366. }
  2367. amd_iommu_stats_init();
  2368. return 0;
  2369. free_domains:
  2370. for_each_iommu(iommu) {
  2371. if (iommu->default_dom)
  2372. dma_ops_domain_free(iommu->default_dom);
  2373. }
  2374. return ret;
  2375. }
  2376. /*****************************************************************************
  2377. *
  2378. * The following functions belong to the exported interface of AMD IOMMU
  2379. *
  2380. * This interface allows access to lower level functions of the IOMMU
  2381. * like protection domain handling and assignement of devices to domains
  2382. * which is not possible with the dma_ops interface.
  2383. *
  2384. *****************************************************************************/
  2385. static void cleanup_domain(struct protection_domain *domain)
  2386. {
  2387. struct iommu_dev_data *entry;
  2388. unsigned long flags;
  2389. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2390. while (!list_empty(&domain->dev_list)) {
  2391. entry = list_first_entry(&domain->dev_list,
  2392. struct iommu_dev_data, list);
  2393. __detach_device(entry);
  2394. atomic_set(&entry->bind, 0);
  2395. }
  2396. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2397. }
  2398. static void protection_domain_free(struct protection_domain *domain)
  2399. {
  2400. if (!domain)
  2401. return;
  2402. del_domain_from_list(domain);
  2403. if (domain->id)
  2404. domain_id_free(domain->id);
  2405. kfree(domain);
  2406. }
  2407. static struct protection_domain *protection_domain_alloc(void)
  2408. {
  2409. struct protection_domain *domain;
  2410. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  2411. if (!domain)
  2412. return NULL;
  2413. spin_lock_init(&domain->lock);
  2414. mutex_init(&domain->api_lock);
  2415. domain->id = domain_id_alloc();
  2416. if (!domain->id)
  2417. goto out_err;
  2418. INIT_LIST_HEAD(&domain->dev_list);
  2419. add_domain_to_list(domain);
  2420. return domain;
  2421. out_err:
  2422. kfree(domain);
  2423. return NULL;
  2424. }
  2425. static int __init alloc_passthrough_domain(void)
  2426. {
  2427. if (pt_domain != NULL)
  2428. return 0;
  2429. /* allocate passthrough domain */
  2430. pt_domain = protection_domain_alloc();
  2431. if (!pt_domain)
  2432. return -ENOMEM;
  2433. pt_domain->mode = PAGE_MODE_NONE;
  2434. return 0;
  2435. }
  2436. static int amd_iommu_domain_init(struct iommu_domain *dom)
  2437. {
  2438. struct protection_domain *domain;
  2439. domain = protection_domain_alloc();
  2440. if (!domain)
  2441. goto out_free;
  2442. domain->mode = PAGE_MODE_3_LEVEL;
  2443. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  2444. if (!domain->pt_root)
  2445. goto out_free;
  2446. domain->iommu_domain = dom;
  2447. dom->priv = domain;
  2448. return 0;
  2449. out_free:
  2450. protection_domain_free(domain);
  2451. return -ENOMEM;
  2452. }
  2453. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  2454. {
  2455. struct protection_domain *domain = dom->priv;
  2456. if (!domain)
  2457. return;
  2458. if (domain->dev_cnt > 0)
  2459. cleanup_domain(domain);
  2460. BUG_ON(domain->dev_cnt != 0);
  2461. if (domain->mode != PAGE_MODE_NONE)
  2462. free_pagetable(domain);
  2463. if (domain->flags & PD_IOMMUV2_MASK)
  2464. free_gcr3_table(domain);
  2465. protection_domain_free(domain);
  2466. dom->priv = NULL;
  2467. }
  2468. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2469. struct device *dev)
  2470. {
  2471. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2472. struct amd_iommu *iommu;
  2473. u16 devid;
  2474. if (!check_device(dev))
  2475. return;
  2476. devid = get_device_id(dev);
  2477. if (dev_data->domain != NULL)
  2478. detach_device(dev);
  2479. iommu = amd_iommu_rlookup_table[devid];
  2480. if (!iommu)
  2481. return;
  2482. iommu_completion_wait(iommu);
  2483. }
  2484. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2485. struct device *dev)
  2486. {
  2487. struct protection_domain *domain = dom->priv;
  2488. struct iommu_dev_data *dev_data;
  2489. struct amd_iommu *iommu;
  2490. int ret;
  2491. if (!check_device(dev))
  2492. return -EINVAL;
  2493. dev_data = dev->archdata.iommu;
  2494. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2495. if (!iommu)
  2496. return -EINVAL;
  2497. if (dev_data->domain)
  2498. detach_device(dev);
  2499. ret = attach_device(dev, domain);
  2500. iommu_completion_wait(iommu);
  2501. return ret;
  2502. }
  2503. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2504. phys_addr_t paddr, size_t page_size, int iommu_prot)
  2505. {
  2506. struct protection_domain *domain = dom->priv;
  2507. int prot = 0;
  2508. int ret;
  2509. if (domain->mode == PAGE_MODE_NONE)
  2510. return -EINVAL;
  2511. if (iommu_prot & IOMMU_READ)
  2512. prot |= IOMMU_PROT_IR;
  2513. if (iommu_prot & IOMMU_WRITE)
  2514. prot |= IOMMU_PROT_IW;
  2515. mutex_lock(&domain->api_lock);
  2516. ret = iommu_map_page(domain, iova, paddr, prot, page_size);
  2517. mutex_unlock(&domain->api_lock);
  2518. return ret;
  2519. }
  2520. static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2521. size_t page_size)
  2522. {
  2523. struct protection_domain *domain = dom->priv;
  2524. size_t unmap_size;
  2525. if (domain->mode == PAGE_MODE_NONE)
  2526. return -EINVAL;
  2527. mutex_lock(&domain->api_lock);
  2528. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2529. mutex_unlock(&domain->api_lock);
  2530. domain_flush_tlb_pde(domain);
  2531. return unmap_size;
  2532. }
  2533. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2534. unsigned long iova)
  2535. {
  2536. struct protection_domain *domain = dom->priv;
  2537. unsigned long offset_mask;
  2538. phys_addr_t paddr;
  2539. u64 *pte, __pte;
  2540. if (domain->mode == PAGE_MODE_NONE)
  2541. return iova;
  2542. pte = fetch_pte(domain, iova);
  2543. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2544. return 0;
  2545. if (PM_PTE_LEVEL(*pte) == 0)
  2546. offset_mask = PAGE_SIZE - 1;
  2547. else
  2548. offset_mask = PTE_PAGE_SIZE(*pte) - 1;
  2549. __pte = *pte & PM_ADDR_MASK;
  2550. paddr = (__pte & ~offset_mask) | (iova & offset_mask);
  2551. return paddr;
  2552. }
  2553. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  2554. unsigned long cap)
  2555. {
  2556. switch (cap) {
  2557. case IOMMU_CAP_CACHE_COHERENCY:
  2558. return 1;
  2559. }
  2560. return 0;
  2561. }
  2562. static struct iommu_ops amd_iommu_ops = {
  2563. .domain_init = amd_iommu_domain_init,
  2564. .domain_destroy = amd_iommu_domain_destroy,
  2565. .attach_dev = amd_iommu_attach_device,
  2566. .detach_dev = amd_iommu_detach_device,
  2567. .map = amd_iommu_map,
  2568. .unmap = amd_iommu_unmap,
  2569. .iova_to_phys = amd_iommu_iova_to_phys,
  2570. .domain_has_cap = amd_iommu_domain_has_cap,
  2571. .pgsize_bitmap = AMD_IOMMU_PGSIZES,
  2572. };
  2573. /*****************************************************************************
  2574. *
  2575. * The next functions do a basic initialization of IOMMU for pass through
  2576. * mode
  2577. *
  2578. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2579. * DMA-API translation.
  2580. *
  2581. *****************************************************************************/
  2582. int __init amd_iommu_init_passthrough(void)
  2583. {
  2584. struct iommu_dev_data *dev_data;
  2585. struct pci_dev *dev = NULL;
  2586. struct amd_iommu *iommu;
  2587. u16 devid;
  2588. int ret;
  2589. ret = alloc_passthrough_domain();
  2590. if (ret)
  2591. return ret;
  2592. for_each_pci_dev(dev) {
  2593. if (!check_device(&dev->dev))
  2594. continue;
  2595. dev_data = get_dev_data(&dev->dev);
  2596. dev_data->passthrough = true;
  2597. devid = get_device_id(&dev->dev);
  2598. iommu = amd_iommu_rlookup_table[devid];
  2599. if (!iommu)
  2600. continue;
  2601. attach_device(&dev->dev, pt_domain);
  2602. }
  2603. amd_iommu_stats_init();
  2604. pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
  2605. return 0;
  2606. }
  2607. /* IOMMUv2 specific functions */
  2608. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  2609. {
  2610. return atomic_notifier_chain_register(&ppr_notifier, nb);
  2611. }
  2612. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  2613. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  2614. {
  2615. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  2616. }
  2617. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2618. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2619. {
  2620. struct protection_domain *domain = dom->priv;
  2621. unsigned long flags;
  2622. spin_lock_irqsave(&domain->lock, flags);
  2623. /* Update data structure */
  2624. domain->mode = PAGE_MODE_NONE;
  2625. domain->updated = true;
  2626. /* Make changes visible to IOMMUs */
  2627. update_domain(domain);
  2628. /* Page-table is not visible to IOMMU anymore, so free it */
  2629. free_pagetable(domain);
  2630. spin_unlock_irqrestore(&domain->lock, flags);
  2631. }
  2632. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2633. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2634. {
  2635. struct protection_domain *domain = dom->priv;
  2636. unsigned long flags;
  2637. int levels, ret;
  2638. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  2639. return -EINVAL;
  2640. /* Number of GCR3 table levels required */
  2641. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2642. levels += 1;
  2643. if (levels > amd_iommu_max_glx_val)
  2644. return -EINVAL;
  2645. spin_lock_irqsave(&domain->lock, flags);
  2646. /*
  2647. * Save us all sanity checks whether devices already in the
  2648. * domain support IOMMUv2. Just force that the domain has no
  2649. * devices attached when it is switched into IOMMUv2 mode.
  2650. */
  2651. ret = -EBUSY;
  2652. if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
  2653. goto out;
  2654. ret = -ENOMEM;
  2655. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2656. if (domain->gcr3_tbl == NULL)
  2657. goto out;
  2658. domain->glx = levels;
  2659. domain->flags |= PD_IOMMUV2_MASK;
  2660. domain->updated = true;
  2661. update_domain(domain);
  2662. ret = 0;
  2663. out:
  2664. spin_unlock_irqrestore(&domain->lock, flags);
  2665. return ret;
  2666. }
  2667. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2668. static int __flush_pasid(struct protection_domain *domain, int pasid,
  2669. u64 address, bool size)
  2670. {
  2671. struct iommu_dev_data *dev_data;
  2672. struct iommu_cmd cmd;
  2673. int i, ret;
  2674. if (!(domain->flags & PD_IOMMUV2_MASK))
  2675. return -EINVAL;
  2676. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2677. /*
  2678. * IOMMU TLB needs to be flushed before Device TLB to
  2679. * prevent device TLB refill from IOMMU TLB
  2680. */
  2681. for (i = 0; i < amd_iommus_present; ++i) {
  2682. if (domain->dev_iommu[i] == 0)
  2683. continue;
  2684. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2685. if (ret != 0)
  2686. goto out;
  2687. }
  2688. /* Wait until IOMMU TLB flushes are complete */
  2689. domain_flush_complete(domain);
  2690. /* Now flush device TLBs */
  2691. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2692. struct amd_iommu *iommu;
  2693. int qdep;
  2694. BUG_ON(!dev_data->ats.enabled);
  2695. qdep = dev_data->ats.qdep;
  2696. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2697. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2698. qdep, address, size);
  2699. ret = iommu_queue_command(iommu, &cmd);
  2700. if (ret != 0)
  2701. goto out;
  2702. }
  2703. /* Wait until all device TLBs are flushed */
  2704. domain_flush_complete(domain);
  2705. ret = 0;
  2706. out:
  2707. return ret;
  2708. }
  2709. static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
  2710. u64 address)
  2711. {
  2712. INC_STATS_COUNTER(invalidate_iotlb);
  2713. return __flush_pasid(domain, pasid, address, false);
  2714. }
  2715. int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
  2716. u64 address)
  2717. {
  2718. struct protection_domain *domain = dom->priv;
  2719. unsigned long flags;
  2720. int ret;
  2721. spin_lock_irqsave(&domain->lock, flags);
  2722. ret = __amd_iommu_flush_page(domain, pasid, address);
  2723. spin_unlock_irqrestore(&domain->lock, flags);
  2724. return ret;
  2725. }
  2726. EXPORT_SYMBOL(amd_iommu_flush_page);
  2727. static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
  2728. {
  2729. INC_STATS_COUNTER(invalidate_iotlb_all);
  2730. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2731. true);
  2732. }
  2733. int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
  2734. {
  2735. struct protection_domain *domain = dom->priv;
  2736. unsigned long flags;
  2737. int ret;
  2738. spin_lock_irqsave(&domain->lock, flags);
  2739. ret = __amd_iommu_flush_tlb(domain, pasid);
  2740. spin_unlock_irqrestore(&domain->lock, flags);
  2741. return ret;
  2742. }
  2743. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2744. static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
  2745. {
  2746. int index;
  2747. u64 *pte;
  2748. while (true) {
  2749. index = (pasid >> (9 * level)) & 0x1ff;
  2750. pte = &root[index];
  2751. if (level == 0)
  2752. break;
  2753. if (!(*pte & GCR3_VALID)) {
  2754. if (!alloc)
  2755. return NULL;
  2756. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2757. if (root == NULL)
  2758. return NULL;
  2759. *pte = __pa(root) | GCR3_VALID;
  2760. }
  2761. root = __va(*pte & PAGE_MASK);
  2762. level -= 1;
  2763. }
  2764. return pte;
  2765. }
  2766. static int __set_gcr3(struct protection_domain *domain, int pasid,
  2767. unsigned long cr3)
  2768. {
  2769. u64 *pte;
  2770. if (domain->mode != PAGE_MODE_NONE)
  2771. return -EINVAL;
  2772. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2773. if (pte == NULL)
  2774. return -ENOMEM;
  2775. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2776. return __amd_iommu_flush_tlb(domain, pasid);
  2777. }
  2778. static int __clear_gcr3(struct protection_domain *domain, int pasid)
  2779. {
  2780. u64 *pte;
  2781. if (domain->mode != PAGE_MODE_NONE)
  2782. return -EINVAL;
  2783. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2784. if (pte == NULL)
  2785. return 0;
  2786. *pte = 0;
  2787. return __amd_iommu_flush_tlb(domain, pasid);
  2788. }
  2789. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
  2790. unsigned long cr3)
  2791. {
  2792. struct protection_domain *domain = dom->priv;
  2793. unsigned long flags;
  2794. int ret;
  2795. spin_lock_irqsave(&domain->lock, flags);
  2796. ret = __set_gcr3(domain, pasid, cr3);
  2797. spin_unlock_irqrestore(&domain->lock, flags);
  2798. return ret;
  2799. }
  2800. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2801. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
  2802. {
  2803. struct protection_domain *domain = dom->priv;
  2804. unsigned long flags;
  2805. int ret;
  2806. spin_lock_irqsave(&domain->lock, flags);
  2807. ret = __clear_gcr3(domain, pasid);
  2808. spin_unlock_irqrestore(&domain->lock, flags);
  2809. return ret;
  2810. }
  2811. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2812. int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
  2813. int status, int tag)
  2814. {
  2815. struct iommu_dev_data *dev_data;
  2816. struct amd_iommu *iommu;
  2817. struct iommu_cmd cmd;
  2818. INC_STATS_COUNTER(complete_ppr);
  2819. dev_data = get_dev_data(&pdev->dev);
  2820. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2821. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2822. tag, dev_data->pri_tlp);
  2823. return iommu_queue_command(iommu, &cmd);
  2824. }
  2825. EXPORT_SYMBOL(amd_iommu_complete_ppr);
  2826. struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
  2827. {
  2828. struct protection_domain *domain;
  2829. domain = get_domain(&pdev->dev);
  2830. if (IS_ERR(domain))
  2831. return NULL;
  2832. /* Only return IOMMUv2 domains */
  2833. if (!(domain->flags & PD_IOMMUV2_MASK))
  2834. return NULL;
  2835. return domain->iommu_domain;
  2836. }
  2837. EXPORT_SYMBOL(amd_iommu_get_v2_domain);
  2838. void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
  2839. {
  2840. struct iommu_dev_data *dev_data;
  2841. if (!amd_iommu_v2_supported())
  2842. return;
  2843. dev_data = get_dev_data(&pdev->dev);
  2844. dev_data->errata |= (1 << erratum);
  2845. }
  2846. EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
  2847. int amd_iommu_device_info(struct pci_dev *pdev,
  2848. struct amd_iommu_device_info *info)
  2849. {
  2850. int max_pasids;
  2851. int pos;
  2852. if (pdev == NULL || info == NULL)
  2853. return -EINVAL;
  2854. if (!amd_iommu_v2_supported())
  2855. return -EINVAL;
  2856. memset(info, 0, sizeof(*info));
  2857. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
  2858. if (pos)
  2859. info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  2860. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  2861. if (pos)
  2862. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  2863. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  2864. if (pos) {
  2865. int features;
  2866. max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
  2867. max_pasids = min(max_pasids, (1 << 20));
  2868. info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  2869. info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
  2870. features = pci_pasid_features(pdev);
  2871. if (features & PCI_PASID_CAP_EXEC)
  2872. info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  2873. if (features & PCI_PASID_CAP_PRIV)
  2874. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  2875. }
  2876. return 0;
  2877. }
  2878. EXPORT_SYMBOL(amd_iommu_device_info);