amd_iommu.c 53 KB

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