amd_iommu.c 60 KB

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