amd_iommu.c 85 KB

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