amd_iommu.c 51 KB

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