amd_iommu.c 46 KB

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