amd_iommu.c 58 KB

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