amd_iommu.c 57 KB

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