amd_iommu.c 58 KB

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