amd_iommu.c 82 KB

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