amd_iommu.c 58 KB

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