amd_iommu.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  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. if (!dev || !dev->dma_mask)
  1117. return false;
  1118. return true;
  1119. }
  1120. /*
  1121. * In this function the list of preallocated protection domains is traversed to
  1122. * find the domain for a specific device
  1123. */
  1124. static struct dma_ops_domain *find_protection_domain(u16 devid)
  1125. {
  1126. struct dma_ops_domain *entry, *ret = NULL;
  1127. unsigned long flags;
  1128. if (list_empty(&iommu_pd_list))
  1129. return NULL;
  1130. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  1131. list_for_each_entry(entry, &iommu_pd_list, list) {
  1132. if (entry->target_dev == devid) {
  1133. ret = entry;
  1134. break;
  1135. }
  1136. }
  1137. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  1138. return ret;
  1139. }
  1140. /*
  1141. * In the dma_ops path we only have the struct device. This function
  1142. * finds the corresponding IOMMU, the protection domain and the
  1143. * requestor id for a given device.
  1144. * If the device is not yet associated with a domain this is also done
  1145. * in this function.
  1146. */
  1147. static int get_device_resources(struct device *dev,
  1148. struct amd_iommu **iommu,
  1149. struct protection_domain **domain,
  1150. u16 *bdf)
  1151. {
  1152. struct dma_ops_domain *dma_dom;
  1153. struct pci_dev *pcidev;
  1154. u16 _bdf;
  1155. *iommu = NULL;
  1156. *domain = NULL;
  1157. *bdf = 0xffff;
  1158. if (dev->bus != &pci_bus_type)
  1159. return 0;
  1160. pcidev = to_pci_dev(dev);
  1161. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1162. /* device not translated by any IOMMU in the system? */
  1163. if (_bdf > amd_iommu_last_bdf)
  1164. return 0;
  1165. *bdf = amd_iommu_alias_table[_bdf];
  1166. *iommu = amd_iommu_rlookup_table[*bdf];
  1167. if (*iommu == NULL)
  1168. return 0;
  1169. *domain = domain_for_device(*bdf);
  1170. if (*domain == NULL) {
  1171. dma_dom = find_protection_domain(*bdf);
  1172. if (!dma_dom)
  1173. dma_dom = (*iommu)->default_dom;
  1174. *domain = &dma_dom->domain;
  1175. attach_device(*iommu, *domain, *bdf);
  1176. DUMP_printk("Using protection domain %d for device %s\n",
  1177. (*domain)->id, dev_name(dev));
  1178. }
  1179. if (domain_for_device(_bdf) == NULL)
  1180. attach_device(*iommu, *domain, _bdf);
  1181. return 1;
  1182. }
  1183. static void update_device_table(struct protection_domain *domain)
  1184. {
  1185. unsigned long flags;
  1186. int i;
  1187. for (i = 0; i <= amd_iommu_last_bdf; ++i) {
  1188. if (amd_iommu_pd_table[i] != domain)
  1189. continue;
  1190. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1191. set_dte_entry(i, domain);
  1192. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1193. }
  1194. }
  1195. static void update_domain(struct protection_domain *domain)
  1196. {
  1197. if (!domain->updated)
  1198. return;
  1199. update_device_table(domain);
  1200. flush_devices_by_domain(domain);
  1201. iommu_flush_tlb_pde(domain);
  1202. domain->updated = false;
  1203. }
  1204. /*
  1205. * This function is used to add another level to an IO page table. Adding
  1206. * another level increases the size of the address space by 9 bits to a size up
  1207. * to 64 bits.
  1208. */
  1209. static bool increase_address_space(struct protection_domain *domain,
  1210. gfp_t gfp)
  1211. {
  1212. u64 *pte;
  1213. if (domain->mode == PAGE_MODE_6_LEVEL)
  1214. /* address space already 64 bit large */
  1215. return false;
  1216. pte = (void *)get_zeroed_page(gfp);
  1217. if (!pte)
  1218. return false;
  1219. *pte = PM_LEVEL_PDE(domain->mode,
  1220. virt_to_phys(domain->pt_root));
  1221. domain->pt_root = pte;
  1222. domain->mode += 1;
  1223. domain->updated = true;
  1224. return true;
  1225. }
  1226. static u64 *alloc_pte(struct protection_domain *domain,
  1227. unsigned long address,
  1228. int end_lvl,
  1229. u64 **pte_page,
  1230. gfp_t gfp)
  1231. {
  1232. u64 *pte, *page;
  1233. int level;
  1234. while (address > PM_LEVEL_SIZE(domain->mode))
  1235. increase_address_space(domain, gfp);
  1236. level = domain->mode - 1;
  1237. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1238. while (level > end_lvl) {
  1239. if (!IOMMU_PTE_PRESENT(*pte)) {
  1240. page = (u64 *)get_zeroed_page(gfp);
  1241. if (!page)
  1242. return NULL;
  1243. *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
  1244. }
  1245. level -= 1;
  1246. pte = IOMMU_PTE_PAGE(*pte);
  1247. if (pte_page && level == end_lvl)
  1248. *pte_page = pte;
  1249. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1250. }
  1251. return pte;
  1252. }
  1253. /*
  1254. * This function fetches the PTE for a given address in the aperture
  1255. */
  1256. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  1257. unsigned long address)
  1258. {
  1259. struct aperture_range *aperture;
  1260. u64 *pte, *pte_page;
  1261. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1262. if (!aperture)
  1263. return NULL;
  1264. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1265. if (!pte) {
  1266. pte = alloc_pte(&dom->domain, address, PM_MAP_4k, &pte_page,
  1267. GFP_ATOMIC);
  1268. aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
  1269. } else
  1270. pte += PM_LEVEL_INDEX(0, address);
  1271. update_domain(&dom->domain);
  1272. return pte;
  1273. }
  1274. /*
  1275. * This is the generic map function. It maps one 4kb page at paddr to
  1276. * the given address in the DMA address space for the domain.
  1277. */
  1278. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  1279. struct dma_ops_domain *dom,
  1280. unsigned long address,
  1281. phys_addr_t paddr,
  1282. int direction)
  1283. {
  1284. u64 *pte, __pte;
  1285. WARN_ON(address > dom->aperture_size);
  1286. paddr &= PAGE_MASK;
  1287. pte = dma_ops_get_pte(dom, address);
  1288. if (!pte)
  1289. return DMA_ERROR_CODE;
  1290. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1291. if (direction == DMA_TO_DEVICE)
  1292. __pte |= IOMMU_PTE_IR;
  1293. else if (direction == DMA_FROM_DEVICE)
  1294. __pte |= IOMMU_PTE_IW;
  1295. else if (direction == DMA_BIDIRECTIONAL)
  1296. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  1297. WARN_ON(*pte);
  1298. *pte = __pte;
  1299. return (dma_addr_t)address;
  1300. }
  1301. /*
  1302. * The generic unmapping function for on page in the DMA address space.
  1303. */
  1304. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  1305. struct dma_ops_domain *dom,
  1306. unsigned long address)
  1307. {
  1308. struct aperture_range *aperture;
  1309. u64 *pte;
  1310. if (address >= dom->aperture_size)
  1311. return;
  1312. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1313. if (!aperture)
  1314. return;
  1315. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1316. if (!pte)
  1317. return;
  1318. pte += PM_LEVEL_INDEX(0, address);
  1319. WARN_ON(!*pte);
  1320. *pte = 0ULL;
  1321. }
  1322. /*
  1323. * This function contains common code for mapping of a physically
  1324. * contiguous memory region into DMA address space. It is used by all
  1325. * mapping functions provided with this IOMMU driver.
  1326. * Must be called with the domain lock held.
  1327. */
  1328. static dma_addr_t __map_single(struct device *dev,
  1329. struct amd_iommu *iommu,
  1330. struct dma_ops_domain *dma_dom,
  1331. phys_addr_t paddr,
  1332. size_t size,
  1333. int dir,
  1334. bool align,
  1335. u64 dma_mask)
  1336. {
  1337. dma_addr_t offset = paddr & ~PAGE_MASK;
  1338. dma_addr_t address, start, ret;
  1339. unsigned int pages;
  1340. unsigned long align_mask = 0;
  1341. int i;
  1342. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1343. paddr &= PAGE_MASK;
  1344. INC_STATS_COUNTER(total_map_requests);
  1345. if (pages > 1)
  1346. INC_STATS_COUNTER(cross_page);
  1347. if (align)
  1348. align_mask = (1UL << get_order(size)) - 1;
  1349. retry:
  1350. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1351. dma_mask);
  1352. if (unlikely(address == DMA_ERROR_CODE)) {
  1353. /*
  1354. * setting next_address here will let the address
  1355. * allocator only scan the new allocated range in the
  1356. * first run. This is a small optimization.
  1357. */
  1358. dma_dom->next_address = dma_dom->aperture_size;
  1359. if (alloc_new_range(iommu, dma_dom, false, GFP_ATOMIC))
  1360. goto out;
  1361. /*
  1362. * aperture was sucessfully enlarged by 128 MB, try
  1363. * allocation again
  1364. */
  1365. goto retry;
  1366. }
  1367. start = address;
  1368. for (i = 0; i < pages; ++i) {
  1369. ret = dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  1370. if (ret == DMA_ERROR_CODE)
  1371. goto out_unmap;
  1372. paddr += PAGE_SIZE;
  1373. start += PAGE_SIZE;
  1374. }
  1375. address += offset;
  1376. ADD_STATS_COUNTER(alloced_io_mem, size);
  1377. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  1378. iommu_flush_tlb(&dma_dom->domain);
  1379. dma_dom->need_flush = false;
  1380. } else if (unlikely(amd_iommu_np_cache))
  1381. iommu_flush_pages(&dma_dom->domain, address, size);
  1382. out:
  1383. return address;
  1384. out_unmap:
  1385. for (--i; i >= 0; --i) {
  1386. start -= PAGE_SIZE;
  1387. dma_ops_domain_unmap(iommu, dma_dom, start);
  1388. }
  1389. dma_ops_free_addresses(dma_dom, address, pages);
  1390. return DMA_ERROR_CODE;
  1391. }
  1392. /*
  1393. * Does the reverse of the __map_single function. Must be called with
  1394. * the domain lock held too
  1395. */
  1396. static void __unmap_single(struct amd_iommu *iommu,
  1397. struct dma_ops_domain *dma_dom,
  1398. dma_addr_t dma_addr,
  1399. size_t size,
  1400. int dir)
  1401. {
  1402. dma_addr_t i, start;
  1403. unsigned int pages;
  1404. if ((dma_addr == DMA_ERROR_CODE) ||
  1405. (dma_addr + size > dma_dom->aperture_size))
  1406. return;
  1407. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1408. dma_addr &= PAGE_MASK;
  1409. start = dma_addr;
  1410. for (i = 0; i < pages; ++i) {
  1411. dma_ops_domain_unmap(iommu, dma_dom, start);
  1412. start += PAGE_SIZE;
  1413. }
  1414. SUB_STATS_COUNTER(alloced_io_mem, size);
  1415. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1416. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1417. iommu_flush_pages(&dma_dom->domain, dma_addr, size);
  1418. dma_dom->need_flush = false;
  1419. }
  1420. }
  1421. /*
  1422. * The exported map_single function for dma_ops.
  1423. */
  1424. static dma_addr_t map_page(struct device *dev, struct page *page,
  1425. unsigned long offset, size_t size,
  1426. enum dma_data_direction dir,
  1427. struct dma_attrs *attrs)
  1428. {
  1429. unsigned long flags;
  1430. struct amd_iommu *iommu;
  1431. struct protection_domain *domain;
  1432. u16 devid;
  1433. dma_addr_t addr;
  1434. u64 dma_mask;
  1435. phys_addr_t paddr = page_to_phys(page) + offset;
  1436. INC_STATS_COUNTER(cnt_map_single);
  1437. if (!check_device(dev))
  1438. return DMA_ERROR_CODE;
  1439. dma_mask = *dev->dma_mask;
  1440. get_device_resources(dev, &iommu, &domain, &devid);
  1441. if (iommu == NULL || domain == NULL)
  1442. /* device not handled by any AMD IOMMU */
  1443. return (dma_addr_t)paddr;
  1444. if (!dma_ops_domain(domain))
  1445. return DMA_ERROR_CODE;
  1446. spin_lock_irqsave(&domain->lock, flags);
  1447. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  1448. dma_mask);
  1449. if (addr == DMA_ERROR_CODE)
  1450. goto out;
  1451. iommu_flush_complete(domain);
  1452. out:
  1453. spin_unlock_irqrestore(&domain->lock, flags);
  1454. return addr;
  1455. }
  1456. /*
  1457. * The exported unmap_single function for dma_ops.
  1458. */
  1459. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1460. enum dma_data_direction dir, struct dma_attrs *attrs)
  1461. {
  1462. unsigned long flags;
  1463. struct amd_iommu *iommu;
  1464. struct protection_domain *domain;
  1465. u16 devid;
  1466. INC_STATS_COUNTER(cnt_unmap_single);
  1467. if (!check_device(dev) ||
  1468. !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 (!check_device(dev))
  1512. return 0;
  1513. dma_mask = *dev->dma_mask;
  1514. get_device_resources(dev, &iommu, &domain, &devid);
  1515. if (!iommu || !domain)
  1516. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1517. if (!dma_ops_domain(domain))
  1518. return 0;
  1519. spin_lock_irqsave(&domain->lock, flags);
  1520. for_each_sg(sglist, s, nelems, i) {
  1521. paddr = sg_phys(s);
  1522. s->dma_address = __map_single(dev, iommu, domain->priv,
  1523. paddr, s->length, dir, false,
  1524. dma_mask);
  1525. if (s->dma_address) {
  1526. s->dma_length = s->length;
  1527. mapped_elems++;
  1528. } else
  1529. goto unmap;
  1530. }
  1531. iommu_flush_complete(domain);
  1532. out:
  1533. spin_unlock_irqrestore(&domain->lock, flags);
  1534. return mapped_elems;
  1535. unmap:
  1536. for_each_sg(sglist, s, mapped_elems, i) {
  1537. if (s->dma_address)
  1538. __unmap_single(iommu, domain->priv, s->dma_address,
  1539. s->dma_length, dir);
  1540. s->dma_address = s->dma_length = 0;
  1541. }
  1542. mapped_elems = 0;
  1543. goto out;
  1544. }
  1545. /*
  1546. * The exported map_sg function for dma_ops (handles scatter-gather
  1547. * lists).
  1548. */
  1549. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1550. int nelems, enum dma_data_direction dir,
  1551. struct dma_attrs *attrs)
  1552. {
  1553. unsigned long flags;
  1554. struct amd_iommu *iommu;
  1555. struct protection_domain *domain;
  1556. struct scatterlist *s;
  1557. u16 devid;
  1558. int i;
  1559. INC_STATS_COUNTER(cnt_unmap_sg);
  1560. if (!check_device(dev) ||
  1561. !get_device_resources(dev, &iommu, &domain, &devid))
  1562. return;
  1563. if (!dma_ops_domain(domain))
  1564. return;
  1565. spin_lock_irqsave(&domain->lock, flags);
  1566. for_each_sg(sglist, s, nelems, i) {
  1567. __unmap_single(iommu, domain->priv, s->dma_address,
  1568. s->dma_length, dir);
  1569. s->dma_address = s->dma_length = 0;
  1570. }
  1571. iommu_flush_complete(domain);
  1572. spin_unlock_irqrestore(&domain->lock, flags);
  1573. }
  1574. /*
  1575. * The exported alloc_coherent function for dma_ops.
  1576. */
  1577. static void *alloc_coherent(struct device *dev, size_t size,
  1578. dma_addr_t *dma_addr, gfp_t flag)
  1579. {
  1580. unsigned long flags;
  1581. void *virt_addr;
  1582. struct amd_iommu *iommu;
  1583. struct protection_domain *domain;
  1584. u16 devid;
  1585. phys_addr_t paddr;
  1586. u64 dma_mask = dev->coherent_dma_mask;
  1587. INC_STATS_COUNTER(cnt_alloc_coherent);
  1588. if (!check_device(dev))
  1589. return NULL;
  1590. if (!get_device_resources(dev, &iommu, &domain, &devid))
  1591. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1592. flag |= __GFP_ZERO;
  1593. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1594. if (!virt_addr)
  1595. return NULL;
  1596. paddr = virt_to_phys(virt_addr);
  1597. if (!iommu || !domain) {
  1598. *dma_addr = (dma_addr_t)paddr;
  1599. return virt_addr;
  1600. }
  1601. if (!dma_ops_domain(domain))
  1602. goto out_free;
  1603. if (!dma_mask)
  1604. dma_mask = *dev->dma_mask;
  1605. spin_lock_irqsave(&domain->lock, flags);
  1606. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  1607. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1608. if (*dma_addr == DMA_ERROR_CODE) {
  1609. spin_unlock_irqrestore(&domain->lock, flags);
  1610. goto out_free;
  1611. }
  1612. iommu_flush_complete(domain);
  1613. spin_unlock_irqrestore(&domain->lock, flags);
  1614. return virt_addr;
  1615. out_free:
  1616. free_pages((unsigned long)virt_addr, get_order(size));
  1617. return NULL;
  1618. }
  1619. /*
  1620. * The exported free_coherent function for dma_ops.
  1621. */
  1622. static void free_coherent(struct device *dev, size_t size,
  1623. void *virt_addr, dma_addr_t dma_addr)
  1624. {
  1625. unsigned long flags;
  1626. struct amd_iommu *iommu;
  1627. struct protection_domain *domain;
  1628. u16 devid;
  1629. INC_STATS_COUNTER(cnt_free_coherent);
  1630. if (!check_device(dev))
  1631. return;
  1632. get_device_resources(dev, &iommu, &domain, &devid);
  1633. if (!iommu || !domain)
  1634. goto free_mem;
  1635. if (!dma_ops_domain(domain))
  1636. goto free_mem;
  1637. spin_lock_irqsave(&domain->lock, flags);
  1638. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1639. iommu_flush_complete(domain);
  1640. spin_unlock_irqrestore(&domain->lock, flags);
  1641. free_mem:
  1642. free_pages((unsigned long)virt_addr, get_order(size));
  1643. }
  1644. /*
  1645. * This function is called by the DMA layer to find out if we can handle a
  1646. * particular device. It is part of the dma_ops.
  1647. */
  1648. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1649. {
  1650. u16 bdf;
  1651. struct pci_dev *pcidev;
  1652. /* No device or no PCI device */
  1653. if (!dev || dev->bus != &pci_bus_type)
  1654. return 0;
  1655. pcidev = to_pci_dev(dev);
  1656. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1657. /* Out of our scope? */
  1658. if (bdf > amd_iommu_last_bdf)
  1659. return 0;
  1660. return 1;
  1661. }
  1662. /*
  1663. * The function for pre-allocating protection domains.
  1664. *
  1665. * If the driver core informs the DMA layer if a driver grabs a device
  1666. * we don't need to preallocate the protection domains anymore.
  1667. * For now we have to.
  1668. */
  1669. static void prealloc_protection_domains(void)
  1670. {
  1671. struct pci_dev *dev = NULL;
  1672. struct dma_ops_domain *dma_dom;
  1673. struct amd_iommu *iommu;
  1674. u16 devid, __devid;
  1675. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1676. __devid = devid = calc_devid(dev->bus->number, dev->devfn);
  1677. if (devid > amd_iommu_last_bdf)
  1678. continue;
  1679. devid = amd_iommu_alias_table[devid];
  1680. if (domain_for_device(devid))
  1681. continue;
  1682. iommu = amd_iommu_rlookup_table[devid];
  1683. if (!iommu)
  1684. continue;
  1685. dma_dom = dma_ops_domain_alloc(iommu);
  1686. if (!dma_dom)
  1687. continue;
  1688. init_unity_mappings_for_device(dma_dom, devid);
  1689. dma_dom->target_dev = devid;
  1690. attach_device(iommu, &dma_dom->domain, devid);
  1691. if (__devid != devid)
  1692. attach_device(iommu, &dma_dom->domain, __devid);
  1693. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1694. }
  1695. }
  1696. static struct dma_map_ops amd_iommu_dma_ops = {
  1697. .alloc_coherent = alloc_coherent,
  1698. .free_coherent = free_coherent,
  1699. .map_page = map_page,
  1700. .unmap_page = unmap_page,
  1701. .map_sg = map_sg,
  1702. .unmap_sg = unmap_sg,
  1703. .dma_supported = amd_iommu_dma_supported,
  1704. };
  1705. /*
  1706. * The function which clues the AMD IOMMU driver into dma_ops.
  1707. */
  1708. int __init amd_iommu_init_dma_ops(void)
  1709. {
  1710. struct amd_iommu *iommu;
  1711. int ret;
  1712. /*
  1713. * first allocate a default protection domain for every IOMMU we
  1714. * found in the system. Devices not assigned to any other
  1715. * protection domain will be assigned to the default one.
  1716. */
  1717. for_each_iommu(iommu) {
  1718. iommu->default_dom = dma_ops_domain_alloc(iommu);
  1719. if (iommu->default_dom == NULL)
  1720. return -ENOMEM;
  1721. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  1722. ret = iommu_init_unity_mappings(iommu);
  1723. if (ret)
  1724. goto free_domains;
  1725. }
  1726. /*
  1727. * If device isolation is enabled, pre-allocate the protection
  1728. * domains for each device.
  1729. */
  1730. if (amd_iommu_isolate)
  1731. prealloc_protection_domains();
  1732. iommu_detected = 1;
  1733. swiotlb = 0;
  1734. #ifdef CONFIG_GART_IOMMU
  1735. gart_iommu_aperture_disabled = 1;
  1736. gart_iommu_aperture = 0;
  1737. #endif
  1738. /* Make the driver finally visible to the drivers */
  1739. dma_ops = &amd_iommu_dma_ops;
  1740. register_iommu(&amd_iommu_ops);
  1741. bus_register_notifier(&pci_bus_type, &device_nb);
  1742. amd_iommu_stats_init();
  1743. return 0;
  1744. free_domains:
  1745. for_each_iommu(iommu) {
  1746. if (iommu->default_dom)
  1747. dma_ops_domain_free(iommu->default_dom);
  1748. }
  1749. return ret;
  1750. }
  1751. /*****************************************************************************
  1752. *
  1753. * The following functions belong to the exported interface of AMD IOMMU
  1754. *
  1755. * This interface allows access to lower level functions of the IOMMU
  1756. * like protection domain handling and assignement of devices to domains
  1757. * which is not possible with the dma_ops interface.
  1758. *
  1759. *****************************************************************************/
  1760. static void cleanup_domain(struct protection_domain *domain)
  1761. {
  1762. unsigned long flags;
  1763. u16 devid;
  1764. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1765. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1766. if (amd_iommu_pd_table[devid] == domain)
  1767. __detach_device(domain, devid);
  1768. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1769. }
  1770. static void protection_domain_free(struct protection_domain *domain)
  1771. {
  1772. if (!domain)
  1773. return;
  1774. del_domain_from_list(domain);
  1775. if (domain->id)
  1776. domain_id_free(domain->id);
  1777. kfree(domain);
  1778. }
  1779. static struct protection_domain *protection_domain_alloc(void)
  1780. {
  1781. struct protection_domain *domain;
  1782. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1783. if (!domain)
  1784. return NULL;
  1785. spin_lock_init(&domain->lock);
  1786. domain->id = domain_id_alloc();
  1787. if (!domain->id)
  1788. goto out_err;
  1789. add_domain_to_list(domain);
  1790. return domain;
  1791. out_err:
  1792. kfree(domain);
  1793. return NULL;
  1794. }
  1795. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1796. {
  1797. struct protection_domain *domain;
  1798. domain = protection_domain_alloc();
  1799. if (!domain)
  1800. goto out_free;
  1801. domain->mode = PAGE_MODE_3_LEVEL;
  1802. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1803. if (!domain->pt_root)
  1804. goto out_free;
  1805. dom->priv = domain;
  1806. return 0;
  1807. out_free:
  1808. protection_domain_free(domain);
  1809. return -ENOMEM;
  1810. }
  1811. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1812. {
  1813. struct protection_domain *domain = dom->priv;
  1814. if (!domain)
  1815. return;
  1816. if (domain->dev_cnt > 0)
  1817. cleanup_domain(domain);
  1818. BUG_ON(domain->dev_cnt != 0);
  1819. free_pagetable(domain);
  1820. domain_id_free(domain->id);
  1821. kfree(domain);
  1822. dom->priv = NULL;
  1823. }
  1824. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1825. struct device *dev)
  1826. {
  1827. struct protection_domain *domain = dom->priv;
  1828. struct amd_iommu *iommu;
  1829. struct pci_dev *pdev;
  1830. u16 devid;
  1831. if (dev->bus != &pci_bus_type)
  1832. return;
  1833. pdev = to_pci_dev(dev);
  1834. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1835. if (devid > 0)
  1836. detach_device(domain, devid);
  1837. iommu = amd_iommu_rlookup_table[devid];
  1838. if (!iommu)
  1839. return;
  1840. iommu_queue_inv_dev_entry(iommu, devid);
  1841. iommu_completion_wait(iommu);
  1842. }
  1843. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1844. struct device *dev)
  1845. {
  1846. struct protection_domain *domain = dom->priv;
  1847. struct protection_domain *old_domain;
  1848. struct amd_iommu *iommu;
  1849. struct pci_dev *pdev;
  1850. u16 devid;
  1851. if (dev->bus != &pci_bus_type)
  1852. return -EINVAL;
  1853. pdev = to_pci_dev(dev);
  1854. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1855. if (devid >= amd_iommu_last_bdf ||
  1856. devid != amd_iommu_alias_table[devid])
  1857. return -EINVAL;
  1858. iommu = amd_iommu_rlookup_table[devid];
  1859. if (!iommu)
  1860. return -EINVAL;
  1861. old_domain = domain_for_device(devid);
  1862. if (old_domain)
  1863. detach_device(old_domain, devid);
  1864. attach_device(iommu, domain, devid);
  1865. iommu_completion_wait(iommu);
  1866. return 0;
  1867. }
  1868. static int amd_iommu_map_range(struct iommu_domain *dom,
  1869. unsigned long iova, phys_addr_t paddr,
  1870. size_t size, int iommu_prot)
  1871. {
  1872. struct protection_domain *domain = dom->priv;
  1873. unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1874. int prot = 0;
  1875. int ret;
  1876. if (iommu_prot & IOMMU_READ)
  1877. prot |= IOMMU_PROT_IR;
  1878. if (iommu_prot & IOMMU_WRITE)
  1879. prot |= IOMMU_PROT_IW;
  1880. iova &= PAGE_MASK;
  1881. paddr &= PAGE_MASK;
  1882. for (i = 0; i < npages; ++i) {
  1883. ret = iommu_map_page(domain, iova, paddr, prot, PM_MAP_4k);
  1884. if (ret)
  1885. return ret;
  1886. iova += PAGE_SIZE;
  1887. paddr += PAGE_SIZE;
  1888. }
  1889. return 0;
  1890. }
  1891. static void amd_iommu_unmap_range(struct iommu_domain *dom,
  1892. unsigned long iova, size_t size)
  1893. {
  1894. struct protection_domain *domain = dom->priv;
  1895. unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
  1896. iova &= PAGE_MASK;
  1897. for (i = 0; i < npages; ++i) {
  1898. iommu_unmap_page(domain, iova, PM_MAP_4k);
  1899. iova += PAGE_SIZE;
  1900. }
  1901. iommu_flush_tlb_pde(domain);
  1902. }
  1903. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1904. unsigned long iova)
  1905. {
  1906. struct protection_domain *domain = dom->priv;
  1907. unsigned long offset = iova & ~PAGE_MASK;
  1908. phys_addr_t paddr;
  1909. u64 *pte;
  1910. pte = fetch_pte(domain, iova, PM_MAP_4k);
  1911. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  1912. return 0;
  1913. paddr = *pte & IOMMU_PAGE_MASK;
  1914. paddr |= offset;
  1915. return paddr;
  1916. }
  1917. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  1918. unsigned long cap)
  1919. {
  1920. return 0;
  1921. }
  1922. static struct iommu_ops amd_iommu_ops = {
  1923. .domain_init = amd_iommu_domain_init,
  1924. .domain_destroy = amd_iommu_domain_destroy,
  1925. .attach_dev = amd_iommu_attach_device,
  1926. .detach_dev = amd_iommu_detach_device,
  1927. .map = amd_iommu_map_range,
  1928. .unmap = amd_iommu_unmap_range,
  1929. .iova_to_phys = amd_iommu_iova_to_phys,
  1930. .domain_has_cap = amd_iommu_domain_has_cap,
  1931. };
  1932. /*****************************************************************************
  1933. *
  1934. * The next functions do a basic initialization of IOMMU for pass through
  1935. * mode
  1936. *
  1937. * In passthrough mode the IOMMU is initialized and enabled but not used for
  1938. * DMA-API translation.
  1939. *
  1940. *****************************************************************************/
  1941. int __init amd_iommu_init_passthrough(void)
  1942. {
  1943. struct pci_dev *dev = NULL;
  1944. u16 devid, devid2;
  1945. /* allocate passthroug domain */
  1946. pt_domain = protection_domain_alloc();
  1947. if (!pt_domain)
  1948. return -ENOMEM;
  1949. pt_domain->mode |= PAGE_MODE_NONE;
  1950. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1951. struct amd_iommu *iommu;
  1952. devid = calc_devid(dev->bus->number, dev->devfn);
  1953. if (devid > amd_iommu_last_bdf)
  1954. continue;
  1955. devid2 = amd_iommu_alias_table[devid];
  1956. iommu = amd_iommu_rlookup_table[devid2];
  1957. if (!iommu)
  1958. continue;
  1959. __attach_device(iommu, pt_domain, devid);
  1960. __attach_device(iommu, pt_domain, devid2);
  1961. }
  1962. pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
  1963. return 0;
  1964. }