amd_iommu.c 53 KB

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