amd_iommu.c 68 KB

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