amd_iommu.c 45 KB

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