amd_iommu.c 60 KB

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