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