amd_iommu.c 57 KB

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