amd_iommu.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/gfp.h>
  21. #include <linux/bitops.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/iommu-helper.h>
  24. #ifdef CONFIG_IOMMU_API
  25. #include <linux/iommu.h>
  26. #endif
  27. #include <asm/proto.h>
  28. #include <asm/iommu.h>
  29. #include <asm/gart.h>
  30. #include <asm/amd_iommu_types.h>
  31. #include <asm/amd_iommu.h>
  32. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  33. #define EXIT_LOOP_COUNT 10000000
  34. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  35. /* A list of preallocated protection domains */
  36. static LIST_HEAD(iommu_pd_list);
  37. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  38. #ifdef CONFIG_IOMMU_API
  39. static struct iommu_ops amd_iommu_ops;
  40. #endif
  41. /*
  42. * general struct to manage commands send to an IOMMU
  43. */
  44. struct iommu_cmd {
  45. u32 data[4];
  46. };
  47. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  48. struct unity_map_entry *e);
  49. static struct dma_ops_domain *find_protection_domain(u16 devid);
  50. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  51. static int iommu_has_npcache(struct amd_iommu *iommu)
  52. {
  53. return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
  54. }
  55. /****************************************************************************
  56. *
  57. * Interrupt handling functions
  58. *
  59. ****************************************************************************/
  60. static void iommu_print_event(void *__evt)
  61. {
  62. u32 *event = __evt;
  63. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  64. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  65. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  66. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  67. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  68. printk(KERN_ERR "AMD IOMMU: Event logged [");
  69. switch (type) {
  70. case EVENT_TYPE_ILL_DEV:
  71. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  72. "address=0x%016llx flags=0x%04x]\n",
  73. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  74. address, flags);
  75. break;
  76. case EVENT_TYPE_IO_FAULT:
  77. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  78. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  79. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  80. domid, address, flags);
  81. break;
  82. case EVENT_TYPE_DEV_TAB_ERR:
  83. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  84. "address=0x%016llx flags=0x%04x]\n",
  85. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  86. address, flags);
  87. break;
  88. case EVENT_TYPE_PAGE_TAB_ERR:
  89. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  90. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  91. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  92. domid, address, flags);
  93. break;
  94. case EVENT_TYPE_ILL_CMD:
  95. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  96. break;
  97. case EVENT_TYPE_CMD_HARD_ERR:
  98. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  99. "flags=0x%04x]\n", address, flags);
  100. break;
  101. case EVENT_TYPE_IOTLB_INV_TO:
  102. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  103. "address=0x%016llx]\n",
  104. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  105. address);
  106. break;
  107. case EVENT_TYPE_INV_DEV_REQ:
  108. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  109. "address=0x%016llx flags=0x%04x]\n",
  110. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  111. address, flags);
  112. break;
  113. default:
  114. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  115. }
  116. }
  117. static void iommu_poll_events(struct amd_iommu *iommu)
  118. {
  119. u32 head, tail;
  120. unsigned long flags;
  121. spin_lock_irqsave(&iommu->lock, flags);
  122. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  123. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  124. while (head != tail) {
  125. iommu_print_event(iommu->evt_buf + head);
  126. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  127. }
  128. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  129. spin_unlock_irqrestore(&iommu->lock, flags);
  130. }
  131. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  132. {
  133. struct amd_iommu *iommu;
  134. list_for_each_entry(iommu, &amd_iommu_list, list)
  135. iommu_poll_events(iommu);
  136. return IRQ_HANDLED;
  137. }
  138. /****************************************************************************
  139. *
  140. * IOMMU command queuing functions
  141. *
  142. ****************************************************************************/
  143. /*
  144. * Writes the command to the IOMMUs command buffer and informs the
  145. * hardware about the new command. Must be called with iommu->lock held.
  146. */
  147. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  148. {
  149. u32 tail, head;
  150. u8 *target;
  151. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  152. target = iommu->cmd_buf + tail;
  153. memcpy_toio(target, cmd, sizeof(*cmd));
  154. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  155. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  156. if (tail == head)
  157. return -ENOMEM;
  158. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  159. return 0;
  160. }
  161. /*
  162. * General queuing function for commands. Takes iommu->lock and calls
  163. * __iommu_queue_command().
  164. */
  165. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  166. {
  167. unsigned long flags;
  168. int ret;
  169. spin_lock_irqsave(&iommu->lock, flags);
  170. ret = __iommu_queue_command(iommu, cmd);
  171. if (!ret)
  172. iommu->need_sync = true;
  173. spin_unlock_irqrestore(&iommu->lock, flags);
  174. return ret;
  175. }
  176. /*
  177. * This function waits until an IOMMU has completed a completion
  178. * wait command
  179. */
  180. static void __iommu_wait_for_completion(struct amd_iommu *iommu)
  181. {
  182. int ready = 0;
  183. unsigned status = 0;
  184. unsigned long i = 0;
  185. while (!ready && (i < EXIT_LOOP_COUNT)) {
  186. ++i;
  187. /* wait for the bit to become one */
  188. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  189. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  190. }
  191. /* set bit back to zero */
  192. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  193. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  194. if (unlikely(i == EXIT_LOOP_COUNT))
  195. panic("AMD IOMMU: Completion wait loop failed\n");
  196. }
  197. /*
  198. * This function queues a completion wait command into the command
  199. * buffer of an IOMMU
  200. */
  201. static int __iommu_completion_wait(struct amd_iommu *iommu)
  202. {
  203. struct iommu_cmd cmd;
  204. memset(&cmd, 0, sizeof(cmd));
  205. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  206. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  207. return __iommu_queue_command(iommu, &cmd);
  208. }
  209. /*
  210. * This function is called whenever we need to ensure that the IOMMU has
  211. * completed execution of all commands we sent. It sends a
  212. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  213. * us about that by writing a value to a physical address we pass with
  214. * the command.
  215. */
  216. static int iommu_completion_wait(struct amd_iommu *iommu)
  217. {
  218. int ret = 0;
  219. unsigned long flags;
  220. spin_lock_irqsave(&iommu->lock, flags);
  221. if (!iommu->need_sync)
  222. goto out;
  223. ret = __iommu_completion_wait(iommu);
  224. iommu->need_sync = false;
  225. if (ret)
  226. goto out;
  227. __iommu_wait_for_completion(iommu);
  228. out:
  229. spin_unlock_irqrestore(&iommu->lock, flags);
  230. return 0;
  231. }
  232. /*
  233. * Command send function for invalidating a device table entry
  234. */
  235. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  236. {
  237. struct iommu_cmd cmd;
  238. int ret;
  239. BUG_ON(iommu == NULL);
  240. memset(&cmd, 0, sizeof(cmd));
  241. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  242. cmd.data[0] = devid;
  243. ret = iommu_queue_command(iommu, &cmd);
  244. return ret;
  245. }
  246. static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  247. u16 domid, int pde, int s)
  248. {
  249. memset(cmd, 0, sizeof(*cmd));
  250. address &= PAGE_MASK;
  251. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  252. cmd->data[1] |= domid;
  253. cmd->data[2] = lower_32_bits(address);
  254. cmd->data[3] = upper_32_bits(address);
  255. if (s) /* size bit - we flush more than one 4kb page */
  256. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  257. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  258. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  259. }
  260. /*
  261. * Generic command send function for invalidaing TLB entries
  262. */
  263. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  264. u64 address, u16 domid, int pde, int s)
  265. {
  266. struct iommu_cmd cmd;
  267. int ret;
  268. __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
  269. ret = iommu_queue_command(iommu, &cmd);
  270. return ret;
  271. }
  272. /*
  273. * TLB invalidation function which is called from the mapping functions.
  274. * It invalidates a single PTE if the range to flush is within a single
  275. * page. Otherwise it flushes the whole TLB of the IOMMU.
  276. */
  277. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  278. u64 address, size_t size)
  279. {
  280. int s = 0;
  281. unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
  282. address &= PAGE_MASK;
  283. if (pages > 1) {
  284. /*
  285. * If we have to flush more than one page, flush all
  286. * TLB entries for this domain
  287. */
  288. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  289. s = 1;
  290. }
  291. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  292. return 0;
  293. }
  294. /* Flush the whole IO/TLB for a given protection domain */
  295. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  296. {
  297. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  298. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  299. }
  300. #ifdef CONFIG_IOMMU_API
  301. /*
  302. * This function is used to flush the IO/TLB for a given protection domain
  303. * on every IOMMU in the system
  304. */
  305. static void iommu_flush_domain(u16 domid)
  306. {
  307. unsigned long flags;
  308. struct amd_iommu *iommu;
  309. struct iommu_cmd cmd;
  310. __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  311. domid, 1, 1);
  312. list_for_each_entry(iommu, &amd_iommu_list, list) {
  313. spin_lock_irqsave(&iommu->lock, flags);
  314. __iommu_queue_command(iommu, &cmd);
  315. __iommu_completion_wait(iommu);
  316. __iommu_wait_for_completion(iommu);
  317. spin_unlock_irqrestore(&iommu->lock, flags);
  318. }
  319. }
  320. #endif
  321. /****************************************************************************
  322. *
  323. * The functions below are used the create the page table mappings for
  324. * unity mapped regions.
  325. *
  326. ****************************************************************************/
  327. /*
  328. * Generic mapping functions. It maps a physical address into a DMA
  329. * address space. It allocates the page table pages if necessary.
  330. * In the future it can be extended to a generic mapping function
  331. * supporting all features of AMD IOMMU page tables like level skipping
  332. * and full 64 bit address spaces.
  333. */
  334. static int iommu_map_page(struct protection_domain *dom,
  335. unsigned long bus_addr,
  336. unsigned long phys_addr,
  337. int prot)
  338. {
  339. u64 __pte, *pte, *page;
  340. bus_addr = PAGE_ALIGN(bus_addr);
  341. phys_addr = PAGE_ALIGN(phys_addr);
  342. /* only support 512GB address spaces for now */
  343. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  344. return -EINVAL;
  345. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  346. if (!IOMMU_PTE_PRESENT(*pte)) {
  347. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  348. if (!page)
  349. return -ENOMEM;
  350. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  351. }
  352. pte = IOMMU_PTE_PAGE(*pte);
  353. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  354. if (!IOMMU_PTE_PRESENT(*pte)) {
  355. page = (u64 *)get_zeroed_page(GFP_KERNEL);
  356. if (!page)
  357. return -ENOMEM;
  358. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  359. }
  360. pte = IOMMU_PTE_PAGE(*pte);
  361. pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)];
  362. if (IOMMU_PTE_PRESENT(*pte))
  363. return -EBUSY;
  364. __pte = phys_addr | IOMMU_PTE_P;
  365. if (prot & IOMMU_PROT_IR)
  366. __pte |= IOMMU_PTE_IR;
  367. if (prot & IOMMU_PROT_IW)
  368. __pte |= IOMMU_PTE_IW;
  369. *pte = __pte;
  370. return 0;
  371. }
  372. #ifdef CONFIG_IOMMU_API
  373. static void iommu_unmap_page(struct protection_domain *dom,
  374. unsigned long bus_addr)
  375. {
  376. u64 *pte;
  377. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  378. if (!IOMMU_PTE_PRESENT(*pte))
  379. return;
  380. pte = IOMMU_PTE_PAGE(*pte);
  381. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  382. if (!IOMMU_PTE_PRESENT(*pte))
  383. return;
  384. pte = IOMMU_PTE_PAGE(*pte);
  385. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  386. *pte = 0;
  387. }
  388. #endif
  389. /*
  390. * This function checks if a specific unity mapping entry is needed for
  391. * this specific IOMMU.
  392. */
  393. static int iommu_for_unity_map(struct amd_iommu *iommu,
  394. struct unity_map_entry *entry)
  395. {
  396. u16 bdf, i;
  397. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  398. bdf = amd_iommu_alias_table[i];
  399. if (amd_iommu_rlookup_table[bdf] == iommu)
  400. return 1;
  401. }
  402. return 0;
  403. }
  404. /*
  405. * Init the unity mappings for a specific IOMMU in the system
  406. *
  407. * Basically iterates over all unity mapping entries and applies them to
  408. * the default domain DMA of that IOMMU if necessary.
  409. */
  410. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  411. {
  412. struct unity_map_entry *entry;
  413. int ret;
  414. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  415. if (!iommu_for_unity_map(iommu, entry))
  416. continue;
  417. ret = dma_ops_unity_map(iommu->default_dom, entry);
  418. if (ret)
  419. return ret;
  420. }
  421. return 0;
  422. }
  423. /*
  424. * This function actually applies the mapping to the page table of the
  425. * dma_ops domain.
  426. */
  427. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  428. struct unity_map_entry *e)
  429. {
  430. u64 addr;
  431. int ret;
  432. for (addr = e->address_start; addr < e->address_end;
  433. addr += PAGE_SIZE) {
  434. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot);
  435. if (ret)
  436. return ret;
  437. /*
  438. * if unity mapping is in aperture range mark the page
  439. * as allocated in the aperture
  440. */
  441. if (addr < dma_dom->aperture_size)
  442. __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap);
  443. }
  444. return 0;
  445. }
  446. /*
  447. * Inits the unity mappings required for a specific device
  448. */
  449. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  450. u16 devid)
  451. {
  452. struct unity_map_entry *e;
  453. int ret;
  454. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  455. if (!(devid >= e->devid_start && devid <= e->devid_end))
  456. continue;
  457. ret = dma_ops_unity_map(dma_dom, e);
  458. if (ret)
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. /****************************************************************************
  464. *
  465. * The next functions belong to the address allocator for the dma_ops
  466. * interface functions. They work like the allocators in the other IOMMU
  467. * drivers. Its basically a bitmap which marks the allocated pages in
  468. * the aperture. Maybe it could be enhanced in the future to a more
  469. * efficient allocator.
  470. *
  471. ****************************************************************************/
  472. /*
  473. * The address allocator core function.
  474. *
  475. * called with domain->lock held
  476. */
  477. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  478. struct dma_ops_domain *dom,
  479. unsigned int pages,
  480. unsigned long align_mask,
  481. u64 dma_mask)
  482. {
  483. unsigned long limit;
  484. unsigned long address;
  485. unsigned long boundary_size;
  486. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  487. PAGE_SIZE) >> PAGE_SHIFT;
  488. limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
  489. dma_mask >> PAGE_SHIFT);
  490. if (dom->next_bit >= limit) {
  491. dom->next_bit = 0;
  492. dom->need_flush = true;
  493. }
  494. address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages,
  495. 0 , boundary_size, align_mask);
  496. if (address == -1) {
  497. address = iommu_area_alloc(dom->bitmap, limit, 0, pages,
  498. 0, boundary_size, align_mask);
  499. dom->need_flush = true;
  500. }
  501. if (likely(address != -1)) {
  502. dom->next_bit = address + pages;
  503. address <<= PAGE_SHIFT;
  504. } else
  505. address = bad_dma_address;
  506. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  507. return address;
  508. }
  509. /*
  510. * The address free function.
  511. *
  512. * called with domain->lock held
  513. */
  514. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  515. unsigned long address,
  516. unsigned int pages)
  517. {
  518. address >>= PAGE_SHIFT;
  519. iommu_area_free(dom->bitmap, address, pages);
  520. if (address >= dom->next_bit)
  521. dom->need_flush = true;
  522. }
  523. /****************************************************************************
  524. *
  525. * The next functions belong to the domain allocation. A domain is
  526. * allocated for every IOMMU as the default domain. If device isolation
  527. * is enabled, every device get its own domain. The most important thing
  528. * about domains is the page table mapping the DMA address space they
  529. * contain.
  530. *
  531. ****************************************************************************/
  532. static u16 domain_id_alloc(void)
  533. {
  534. unsigned long flags;
  535. int id;
  536. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  537. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  538. BUG_ON(id == 0);
  539. if (id > 0 && id < MAX_DOMAIN_ID)
  540. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  541. else
  542. id = 0;
  543. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  544. return id;
  545. }
  546. #ifdef CONFIG_IOMMU_API
  547. static void domain_id_free(int id)
  548. {
  549. unsigned long flags;
  550. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  551. if (id > 0 && id < MAX_DOMAIN_ID)
  552. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  553. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  554. }
  555. #endif
  556. /*
  557. * Used to reserve address ranges in the aperture (e.g. for exclusion
  558. * ranges.
  559. */
  560. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  561. unsigned long start_page,
  562. unsigned int pages)
  563. {
  564. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  565. if (start_page + pages > last_page)
  566. pages = last_page - start_page;
  567. iommu_area_reserve(dom->bitmap, start_page, pages);
  568. }
  569. static void free_pagetable(struct protection_domain *domain)
  570. {
  571. int i, j;
  572. u64 *p1, *p2, *p3;
  573. p1 = domain->pt_root;
  574. if (!p1)
  575. return;
  576. for (i = 0; i < 512; ++i) {
  577. if (!IOMMU_PTE_PRESENT(p1[i]))
  578. continue;
  579. p2 = IOMMU_PTE_PAGE(p1[i]);
  580. for (j = 0; j < 512; ++j) {
  581. if (!IOMMU_PTE_PRESENT(p2[j]))
  582. continue;
  583. p3 = IOMMU_PTE_PAGE(p2[j]);
  584. free_page((unsigned long)p3);
  585. }
  586. free_page((unsigned long)p2);
  587. }
  588. free_page((unsigned long)p1);
  589. domain->pt_root = NULL;
  590. }
  591. /*
  592. * Free a domain, only used if something went wrong in the
  593. * allocation path and we need to free an already allocated page table
  594. */
  595. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  596. {
  597. if (!dom)
  598. return;
  599. free_pagetable(&dom->domain);
  600. kfree(dom->pte_pages);
  601. kfree(dom->bitmap);
  602. kfree(dom);
  603. }
  604. /*
  605. * Allocates a new protection domain usable for the dma_ops functions.
  606. * It also intializes the page table and the address allocator data
  607. * structures required for the dma_ops interface
  608. */
  609. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  610. unsigned order)
  611. {
  612. struct dma_ops_domain *dma_dom;
  613. unsigned i, num_pte_pages;
  614. u64 *l2_pde;
  615. u64 address;
  616. /*
  617. * Currently the DMA aperture must be between 32 MB and 1GB in size
  618. */
  619. if ((order < 25) || (order > 30))
  620. return NULL;
  621. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  622. if (!dma_dom)
  623. return NULL;
  624. spin_lock_init(&dma_dom->domain.lock);
  625. dma_dom->domain.id = domain_id_alloc();
  626. if (dma_dom->domain.id == 0)
  627. goto free_dma_dom;
  628. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  629. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  630. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  631. dma_dom->domain.priv = dma_dom;
  632. if (!dma_dom->domain.pt_root)
  633. goto free_dma_dom;
  634. dma_dom->aperture_size = (1ULL << order);
  635. dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8),
  636. GFP_KERNEL);
  637. if (!dma_dom->bitmap)
  638. goto free_dma_dom;
  639. /*
  640. * mark the first page as allocated so we never return 0 as
  641. * a valid dma-address. So we can use 0 as error value
  642. */
  643. dma_dom->bitmap[0] = 1;
  644. dma_dom->next_bit = 0;
  645. dma_dom->need_flush = false;
  646. dma_dom->target_dev = 0xffff;
  647. /* Intialize the exclusion range if necessary */
  648. if (iommu->exclusion_start &&
  649. iommu->exclusion_start < dma_dom->aperture_size) {
  650. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  651. int pages = iommu_num_pages(iommu->exclusion_start,
  652. iommu->exclusion_length,
  653. PAGE_SIZE);
  654. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  655. }
  656. /*
  657. * At the last step, build the page tables so we don't need to
  658. * allocate page table pages in the dma_ops mapping/unmapping
  659. * path.
  660. */
  661. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  662. dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *),
  663. GFP_KERNEL);
  664. if (!dma_dom->pte_pages)
  665. goto free_dma_dom;
  666. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  667. if (l2_pde == NULL)
  668. goto free_dma_dom;
  669. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  670. for (i = 0; i < num_pte_pages; ++i) {
  671. dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL);
  672. if (!dma_dom->pte_pages[i])
  673. goto free_dma_dom;
  674. address = virt_to_phys(dma_dom->pte_pages[i]);
  675. l2_pde[i] = IOMMU_L1_PDE(address);
  676. }
  677. return dma_dom;
  678. free_dma_dom:
  679. dma_ops_domain_free(dma_dom);
  680. return NULL;
  681. }
  682. /*
  683. * little helper function to check whether a given protection domain is a
  684. * dma_ops domain
  685. */
  686. static bool dma_ops_domain(struct protection_domain *domain)
  687. {
  688. return domain->flags & PD_DMA_OPS_MASK;
  689. }
  690. /*
  691. * Find out the protection domain structure for a given PCI device. This
  692. * will give us the pointer to the page table root for example.
  693. */
  694. static struct protection_domain *domain_for_device(u16 devid)
  695. {
  696. struct protection_domain *dom;
  697. unsigned long flags;
  698. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  699. dom = amd_iommu_pd_table[devid];
  700. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  701. return dom;
  702. }
  703. /*
  704. * If a device is not yet associated with a domain, this function does
  705. * assigns it visible for the hardware
  706. */
  707. static void attach_device(struct amd_iommu *iommu,
  708. struct protection_domain *domain,
  709. u16 devid)
  710. {
  711. unsigned long flags;
  712. u64 pte_root = virt_to_phys(domain->pt_root);
  713. domain->dev_cnt += 1;
  714. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  715. << DEV_ENTRY_MODE_SHIFT;
  716. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  717. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  718. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  719. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  720. amd_iommu_dev_table[devid].data[2] = domain->id;
  721. amd_iommu_pd_table[devid] = domain;
  722. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  723. iommu_queue_inv_dev_entry(iommu, devid);
  724. }
  725. /*
  726. * Removes a device from a protection domain (unlocked)
  727. */
  728. static void __detach_device(struct protection_domain *domain, u16 devid)
  729. {
  730. /* lock domain */
  731. spin_lock(&domain->lock);
  732. /* remove domain from the lookup table */
  733. amd_iommu_pd_table[devid] = NULL;
  734. /* remove entry from the device table seen by the hardware */
  735. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  736. amd_iommu_dev_table[devid].data[1] = 0;
  737. amd_iommu_dev_table[devid].data[2] = 0;
  738. /* decrease reference counter */
  739. domain->dev_cnt -= 1;
  740. /* ready */
  741. spin_unlock(&domain->lock);
  742. }
  743. /*
  744. * Removes a device from a protection domain (with devtable_lock held)
  745. */
  746. static void detach_device(struct protection_domain *domain, u16 devid)
  747. {
  748. unsigned long flags;
  749. /* lock device table */
  750. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  751. __detach_device(domain, devid);
  752. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  753. }
  754. static int device_change_notifier(struct notifier_block *nb,
  755. unsigned long action, void *data)
  756. {
  757. struct device *dev = data;
  758. struct pci_dev *pdev = to_pci_dev(dev);
  759. u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
  760. struct protection_domain *domain;
  761. struct dma_ops_domain *dma_domain;
  762. struct amd_iommu *iommu;
  763. int order = amd_iommu_aperture_order;
  764. unsigned long flags;
  765. if (devid > amd_iommu_last_bdf)
  766. goto out;
  767. devid = amd_iommu_alias_table[devid];
  768. iommu = amd_iommu_rlookup_table[devid];
  769. if (iommu == NULL)
  770. goto out;
  771. domain = domain_for_device(devid);
  772. if (domain && !dma_ops_domain(domain))
  773. WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
  774. "to a non-dma-ops domain\n", dev_name(dev));
  775. switch (action) {
  776. case BUS_NOTIFY_BOUND_DRIVER:
  777. if (domain)
  778. goto out;
  779. dma_domain = find_protection_domain(devid);
  780. if (!dma_domain)
  781. dma_domain = iommu->default_dom;
  782. attach_device(iommu, &dma_domain->domain, devid);
  783. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  784. "device %s\n", dma_domain->domain.id, dev_name(dev));
  785. break;
  786. case BUS_NOTIFY_UNBIND_DRIVER:
  787. if (!domain)
  788. goto out;
  789. detach_device(domain, devid);
  790. break;
  791. case BUS_NOTIFY_ADD_DEVICE:
  792. /* allocate a protection domain if a device is added */
  793. dma_domain = find_protection_domain(devid);
  794. if (dma_domain)
  795. goto out;
  796. dma_domain = dma_ops_domain_alloc(iommu, order);
  797. if (!dma_domain)
  798. goto out;
  799. dma_domain->target_dev = devid;
  800. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  801. list_add_tail(&dma_domain->list, &iommu_pd_list);
  802. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  803. break;
  804. default:
  805. goto out;
  806. }
  807. iommu_queue_inv_dev_entry(iommu, devid);
  808. iommu_completion_wait(iommu);
  809. out:
  810. return 0;
  811. }
  812. struct notifier_block device_nb = {
  813. .notifier_call = device_change_notifier,
  814. };
  815. /*****************************************************************************
  816. *
  817. * The next functions belong to the dma_ops mapping/unmapping code.
  818. *
  819. *****************************************************************************/
  820. /*
  821. * This function checks if the driver got a valid device from the caller to
  822. * avoid dereferencing invalid pointers.
  823. */
  824. static bool check_device(struct device *dev)
  825. {
  826. if (!dev || !dev->dma_mask)
  827. return false;
  828. return true;
  829. }
  830. /*
  831. * In this function the list of preallocated protection domains is traversed to
  832. * find the domain for a specific device
  833. */
  834. static struct dma_ops_domain *find_protection_domain(u16 devid)
  835. {
  836. struct dma_ops_domain *entry, *ret = NULL;
  837. unsigned long flags;
  838. if (list_empty(&iommu_pd_list))
  839. return NULL;
  840. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  841. list_for_each_entry(entry, &iommu_pd_list, list) {
  842. if (entry->target_dev == devid) {
  843. ret = entry;
  844. break;
  845. }
  846. }
  847. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  848. return ret;
  849. }
  850. /*
  851. * In the dma_ops path we only have the struct device. This function
  852. * finds the corresponding IOMMU, the protection domain and the
  853. * requestor id for a given device.
  854. * If the device is not yet associated with a domain this is also done
  855. * in this function.
  856. */
  857. static int get_device_resources(struct device *dev,
  858. struct amd_iommu **iommu,
  859. struct protection_domain **domain,
  860. u16 *bdf)
  861. {
  862. struct dma_ops_domain *dma_dom;
  863. struct pci_dev *pcidev;
  864. u16 _bdf;
  865. *iommu = NULL;
  866. *domain = NULL;
  867. *bdf = 0xffff;
  868. if (dev->bus != &pci_bus_type)
  869. return 0;
  870. pcidev = to_pci_dev(dev);
  871. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  872. /* device not translated by any IOMMU in the system? */
  873. if (_bdf > amd_iommu_last_bdf)
  874. return 0;
  875. *bdf = amd_iommu_alias_table[_bdf];
  876. *iommu = amd_iommu_rlookup_table[*bdf];
  877. if (*iommu == NULL)
  878. return 0;
  879. *domain = domain_for_device(*bdf);
  880. if (*domain == NULL) {
  881. dma_dom = find_protection_domain(*bdf);
  882. if (!dma_dom)
  883. dma_dom = (*iommu)->default_dom;
  884. *domain = &dma_dom->domain;
  885. attach_device(*iommu, *domain, *bdf);
  886. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  887. "device %s\n", (*domain)->id, dev_name(dev));
  888. }
  889. if (domain_for_device(_bdf) == NULL)
  890. attach_device(*iommu, *domain, _bdf);
  891. return 1;
  892. }
  893. /*
  894. * This is the generic map function. It maps one 4kb page at paddr to
  895. * the given address in the DMA address space for the domain.
  896. */
  897. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  898. struct dma_ops_domain *dom,
  899. unsigned long address,
  900. phys_addr_t paddr,
  901. int direction)
  902. {
  903. u64 *pte, __pte;
  904. WARN_ON(address > dom->aperture_size);
  905. paddr &= PAGE_MASK;
  906. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  907. pte += IOMMU_PTE_L0_INDEX(address);
  908. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  909. if (direction == DMA_TO_DEVICE)
  910. __pte |= IOMMU_PTE_IR;
  911. else if (direction == DMA_FROM_DEVICE)
  912. __pte |= IOMMU_PTE_IW;
  913. else if (direction == DMA_BIDIRECTIONAL)
  914. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  915. WARN_ON(*pte);
  916. *pte = __pte;
  917. return (dma_addr_t)address;
  918. }
  919. /*
  920. * The generic unmapping function for on page in the DMA address space.
  921. */
  922. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  923. struct dma_ops_domain *dom,
  924. unsigned long address)
  925. {
  926. u64 *pte;
  927. if (address >= dom->aperture_size)
  928. return;
  929. WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
  930. pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  931. pte += IOMMU_PTE_L0_INDEX(address);
  932. WARN_ON(!*pte);
  933. *pte = 0ULL;
  934. }
  935. /*
  936. * This function contains common code for mapping of a physically
  937. * contiguous memory region into DMA address space. It is used by all
  938. * mapping functions provided with this IOMMU driver.
  939. * Must be called with the domain lock held.
  940. */
  941. static dma_addr_t __map_single(struct device *dev,
  942. struct amd_iommu *iommu,
  943. struct dma_ops_domain *dma_dom,
  944. phys_addr_t paddr,
  945. size_t size,
  946. int dir,
  947. bool align,
  948. u64 dma_mask)
  949. {
  950. dma_addr_t offset = paddr & ~PAGE_MASK;
  951. dma_addr_t address, start;
  952. unsigned int pages;
  953. unsigned long align_mask = 0;
  954. int i;
  955. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  956. paddr &= PAGE_MASK;
  957. if (align)
  958. align_mask = (1UL << get_order(size)) - 1;
  959. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  960. dma_mask);
  961. if (unlikely(address == bad_dma_address))
  962. goto out;
  963. start = address;
  964. for (i = 0; i < pages; ++i) {
  965. dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  966. paddr += PAGE_SIZE;
  967. start += PAGE_SIZE;
  968. }
  969. address += offset;
  970. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  971. iommu_flush_tlb(iommu, dma_dom->domain.id);
  972. dma_dom->need_flush = false;
  973. } else if (unlikely(iommu_has_npcache(iommu)))
  974. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  975. out:
  976. return address;
  977. }
  978. /*
  979. * Does the reverse of the __map_single function. Must be called with
  980. * the domain lock held too
  981. */
  982. static void __unmap_single(struct amd_iommu *iommu,
  983. struct dma_ops_domain *dma_dom,
  984. dma_addr_t dma_addr,
  985. size_t size,
  986. int dir)
  987. {
  988. dma_addr_t i, start;
  989. unsigned int pages;
  990. if ((dma_addr == bad_dma_address) ||
  991. (dma_addr + size > dma_dom->aperture_size))
  992. return;
  993. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  994. dma_addr &= PAGE_MASK;
  995. start = dma_addr;
  996. for (i = 0; i < pages; ++i) {
  997. dma_ops_domain_unmap(iommu, dma_dom, start);
  998. start += PAGE_SIZE;
  999. }
  1000. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1001. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1002. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  1003. dma_dom->need_flush = false;
  1004. }
  1005. }
  1006. /*
  1007. * The exported map_single function for dma_ops.
  1008. */
  1009. static dma_addr_t map_single(struct device *dev, phys_addr_t paddr,
  1010. size_t size, int dir)
  1011. {
  1012. unsigned long flags;
  1013. struct amd_iommu *iommu;
  1014. struct protection_domain *domain;
  1015. u16 devid;
  1016. dma_addr_t addr;
  1017. u64 dma_mask;
  1018. if (!check_device(dev))
  1019. return bad_dma_address;
  1020. dma_mask = *dev->dma_mask;
  1021. get_device_resources(dev, &iommu, &domain, &devid);
  1022. if (iommu == NULL || domain == NULL)
  1023. /* device not handled by any AMD IOMMU */
  1024. return (dma_addr_t)paddr;
  1025. if (!dma_ops_domain(domain))
  1026. return bad_dma_address;
  1027. spin_lock_irqsave(&domain->lock, flags);
  1028. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  1029. dma_mask);
  1030. if (addr == bad_dma_address)
  1031. goto out;
  1032. iommu_completion_wait(iommu);
  1033. out:
  1034. spin_unlock_irqrestore(&domain->lock, flags);
  1035. return addr;
  1036. }
  1037. /*
  1038. * The exported unmap_single function for dma_ops.
  1039. */
  1040. static void unmap_single(struct device *dev, dma_addr_t dma_addr,
  1041. size_t size, int dir)
  1042. {
  1043. unsigned long flags;
  1044. struct amd_iommu *iommu;
  1045. struct protection_domain *domain;
  1046. u16 devid;
  1047. if (!check_device(dev) ||
  1048. !get_device_resources(dev, &iommu, &domain, &devid))
  1049. /* device not handled by any AMD IOMMU */
  1050. return;
  1051. if (!dma_ops_domain(domain))
  1052. return;
  1053. spin_lock_irqsave(&domain->lock, flags);
  1054. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  1055. iommu_completion_wait(iommu);
  1056. spin_unlock_irqrestore(&domain->lock, flags);
  1057. }
  1058. /*
  1059. * This is a special map_sg function which is used if we should map a
  1060. * device which is not handled by an AMD IOMMU in the system.
  1061. */
  1062. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1063. int nelems, int dir)
  1064. {
  1065. struct scatterlist *s;
  1066. int i;
  1067. for_each_sg(sglist, s, nelems, i) {
  1068. s->dma_address = (dma_addr_t)sg_phys(s);
  1069. s->dma_length = s->length;
  1070. }
  1071. return nelems;
  1072. }
  1073. /*
  1074. * The exported map_sg function for dma_ops (handles scatter-gather
  1075. * lists).
  1076. */
  1077. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1078. int nelems, int dir)
  1079. {
  1080. unsigned long flags;
  1081. struct amd_iommu *iommu;
  1082. struct protection_domain *domain;
  1083. u16 devid;
  1084. int i;
  1085. struct scatterlist *s;
  1086. phys_addr_t paddr;
  1087. int mapped_elems = 0;
  1088. u64 dma_mask;
  1089. if (!check_device(dev))
  1090. return 0;
  1091. dma_mask = *dev->dma_mask;
  1092. get_device_resources(dev, &iommu, &domain, &devid);
  1093. if (!iommu || !domain)
  1094. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1095. if (!dma_ops_domain(domain))
  1096. return 0;
  1097. spin_lock_irqsave(&domain->lock, flags);
  1098. for_each_sg(sglist, s, nelems, i) {
  1099. paddr = sg_phys(s);
  1100. s->dma_address = __map_single(dev, iommu, domain->priv,
  1101. paddr, s->length, dir, false,
  1102. dma_mask);
  1103. if (s->dma_address) {
  1104. s->dma_length = s->length;
  1105. mapped_elems++;
  1106. } else
  1107. goto unmap;
  1108. }
  1109. iommu_completion_wait(iommu);
  1110. out:
  1111. spin_unlock_irqrestore(&domain->lock, flags);
  1112. return mapped_elems;
  1113. unmap:
  1114. for_each_sg(sglist, s, mapped_elems, i) {
  1115. if (s->dma_address)
  1116. __unmap_single(iommu, domain->priv, s->dma_address,
  1117. s->dma_length, dir);
  1118. s->dma_address = s->dma_length = 0;
  1119. }
  1120. mapped_elems = 0;
  1121. goto out;
  1122. }
  1123. /*
  1124. * The exported map_sg function for dma_ops (handles scatter-gather
  1125. * lists).
  1126. */
  1127. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1128. int nelems, int dir)
  1129. {
  1130. unsigned long flags;
  1131. struct amd_iommu *iommu;
  1132. struct protection_domain *domain;
  1133. struct scatterlist *s;
  1134. u16 devid;
  1135. int i;
  1136. if (!check_device(dev) ||
  1137. !get_device_resources(dev, &iommu, &domain, &devid))
  1138. return;
  1139. if (!dma_ops_domain(domain))
  1140. return;
  1141. spin_lock_irqsave(&domain->lock, flags);
  1142. for_each_sg(sglist, s, nelems, i) {
  1143. __unmap_single(iommu, domain->priv, s->dma_address,
  1144. s->dma_length, dir);
  1145. s->dma_address = s->dma_length = 0;
  1146. }
  1147. iommu_completion_wait(iommu);
  1148. spin_unlock_irqrestore(&domain->lock, flags);
  1149. }
  1150. /*
  1151. * The exported alloc_coherent function for dma_ops.
  1152. */
  1153. static void *alloc_coherent(struct device *dev, size_t size,
  1154. dma_addr_t *dma_addr, gfp_t flag)
  1155. {
  1156. unsigned long flags;
  1157. void *virt_addr;
  1158. struct amd_iommu *iommu;
  1159. struct protection_domain *domain;
  1160. u16 devid;
  1161. phys_addr_t paddr;
  1162. u64 dma_mask = dev->coherent_dma_mask;
  1163. if (!check_device(dev))
  1164. return NULL;
  1165. if (!get_device_resources(dev, &iommu, &domain, &devid))
  1166. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1167. flag |= __GFP_ZERO;
  1168. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1169. if (!virt_addr)
  1170. return 0;
  1171. paddr = virt_to_phys(virt_addr);
  1172. if (!iommu || !domain) {
  1173. *dma_addr = (dma_addr_t)paddr;
  1174. return virt_addr;
  1175. }
  1176. if (!dma_ops_domain(domain))
  1177. goto out_free;
  1178. if (!dma_mask)
  1179. dma_mask = *dev->dma_mask;
  1180. spin_lock_irqsave(&domain->lock, flags);
  1181. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  1182. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1183. if (*dma_addr == bad_dma_address)
  1184. goto out_free;
  1185. iommu_completion_wait(iommu);
  1186. spin_unlock_irqrestore(&domain->lock, flags);
  1187. return virt_addr;
  1188. out_free:
  1189. free_pages((unsigned long)virt_addr, get_order(size));
  1190. return NULL;
  1191. }
  1192. /*
  1193. * The exported free_coherent function for dma_ops.
  1194. */
  1195. static void free_coherent(struct device *dev, size_t size,
  1196. void *virt_addr, dma_addr_t dma_addr)
  1197. {
  1198. unsigned long flags;
  1199. struct amd_iommu *iommu;
  1200. struct protection_domain *domain;
  1201. u16 devid;
  1202. if (!check_device(dev))
  1203. return;
  1204. get_device_resources(dev, &iommu, &domain, &devid);
  1205. if (!iommu || !domain)
  1206. goto free_mem;
  1207. if (!dma_ops_domain(domain))
  1208. goto free_mem;
  1209. spin_lock_irqsave(&domain->lock, flags);
  1210. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1211. iommu_completion_wait(iommu);
  1212. spin_unlock_irqrestore(&domain->lock, flags);
  1213. free_mem:
  1214. free_pages((unsigned long)virt_addr, get_order(size));
  1215. }
  1216. /*
  1217. * This function is called by the DMA layer to find out if we can handle a
  1218. * particular device. It is part of the dma_ops.
  1219. */
  1220. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1221. {
  1222. u16 bdf;
  1223. struct pci_dev *pcidev;
  1224. /* No device or no PCI device */
  1225. if (!dev || dev->bus != &pci_bus_type)
  1226. return 0;
  1227. pcidev = to_pci_dev(dev);
  1228. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1229. /* Out of our scope? */
  1230. if (bdf > amd_iommu_last_bdf)
  1231. return 0;
  1232. return 1;
  1233. }
  1234. /*
  1235. * The function for pre-allocating protection domains.
  1236. *
  1237. * If the driver core informs the DMA layer if a driver grabs a device
  1238. * we don't need to preallocate the protection domains anymore.
  1239. * For now we have to.
  1240. */
  1241. void prealloc_protection_domains(void)
  1242. {
  1243. struct pci_dev *dev = NULL;
  1244. struct dma_ops_domain *dma_dom;
  1245. struct amd_iommu *iommu;
  1246. int order = amd_iommu_aperture_order;
  1247. u16 devid;
  1248. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1249. devid = calc_devid(dev->bus->number, dev->devfn);
  1250. if (devid > amd_iommu_last_bdf)
  1251. continue;
  1252. devid = amd_iommu_alias_table[devid];
  1253. if (domain_for_device(devid))
  1254. continue;
  1255. iommu = amd_iommu_rlookup_table[devid];
  1256. if (!iommu)
  1257. continue;
  1258. dma_dom = dma_ops_domain_alloc(iommu, order);
  1259. if (!dma_dom)
  1260. continue;
  1261. init_unity_mappings_for_device(dma_dom, devid);
  1262. dma_dom->target_dev = devid;
  1263. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1264. }
  1265. }
  1266. static struct dma_mapping_ops amd_iommu_dma_ops = {
  1267. .alloc_coherent = alloc_coherent,
  1268. .free_coherent = free_coherent,
  1269. .map_single = map_single,
  1270. .unmap_single = unmap_single,
  1271. .map_sg = map_sg,
  1272. .unmap_sg = unmap_sg,
  1273. .dma_supported = amd_iommu_dma_supported,
  1274. };
  1275. /*
  1276. * The function which clues the AMD IOMMU driver into dma_ops.
  1277. */
  1278. int __init amd_iommu_init_dma_ops(void)
  1279. {
  1280. struct amd_iommu *iommu;
  1281. int order = amd_iommu_aperture_order;
  1282. int ret;
  1283. /*
  1284. * first allocate a default protection domain for every IOMMU we
  1285. * found in the system. Devices not assigned to any other
  1286. * protection domain will be assigned to the default one.
  1287. */
  1288. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1289. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1290. if (iommu->default_dom == NULL)
  1291. return -ENOMEM;
  1292. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  1293. ret = iommu_init_unity_mappings(iommu);
  1294. if (ret)
  1295. goto free_domains;
  1296. }
  1297. /*
  1298. * If device isolation is enabled, pre-allocate the protection
  1299. * domains for each device.
  1300. */
  1301. if (amd_iommu_isolate)
  1302. prealloc_protection_domains();
  1303. iommu_detected = 1;
  1304. force_iommu = 1;
  1305. bad_dma_address = 0;
  1306. #ifdef CONFIG_GART_IOMMU
  1307. gart_iommu_aperture_disabled = 1;
  1308. gart_iommu_aperture = 0;
  1309. #endif
  1310. /* Make the driver finally visible to the drivers */
  1311. dma_ops = &amd_iommu_dma_ops;
  1312. #ifdef CONFIG_IOMMU_API
  1313. register_iommu(&amd_iommu_ops);
  1314. #endif
  1315. bus_register_notifier(&pci_bus_type, &device_nb);
  1316. return 0;
  1317. free_domains:
  1318. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1319. if (iommu->default_dom)
  1320. dma_ops_domain_free(iommu->default_dom);
  1321. }
  1322. return ret;
  1323. }
  1324. /*****************************************************************************
  1325. *
  1326. * The following functions belong to the exported interface of AMD IOMMU
  1327. *
  1328. * This interface allows access to lower level functions of the IOMMU
  1329. * like protection domain handling and assignement of devices to domains
  1330. * which is not possible with the dma_ops interface.
  1331. *
  1332. *****************************************************************************/
  1333. #ifdef CONFIG_IOMMU_API
  1334. static void cleanup_domain(struct protection_domain *domain)
  1335. {
  1336. unsigned long flags;
  1337. u16 devid;
  1338. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1339. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1340. if (amd_iommu_pd_table[devid] == domain)
  1341. __detach_device(domain, devid);
  1342. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1343. }
  1344. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1345. {
  1346. struct protection_domain *domain;
  1347. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1348. if (!domain)
  1349. return -ENOMEM;
  1350. spin_lock_init(&domain->lock);
  1351. domain->mode = PAGE_MODE_3_LEVEL;
  1352. domain->id = domain_id_alloc();
  1353. if (!domain->id)
  1354. goto out_free;
  1355. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1356. if (!domain->pt_root)
  1357. goto out_free;
  1358. dom->priv = domain;
  1359. return 0;
  1360. out_free:
  1361. kfree(domain);
  1362. return -ENOMEM;
  1363. }
  1364. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1365. {
  1366. struct protection_domain *domain = dom->priv;
  1367. if (!domain)
  1368. return;
  1369. if (domain->dev_cnt > 0)
  1370. cleanup_domain(domain);
  1371. BUG_ON(domain->dev_cnt != 0);
  1372. free_pagetable(domain);
  1373. domain_id_free(domain->id);
  1374. kfree(domain);
  1375. dom->priv = NULL;
  1376. }
  1377. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1378. struct device *dev)
  1379. {
  1380. struct protection_domain *domain = dom->priv;
  1381. struct amd_iommu *iommu;
  1382. struct pci_dev *pdev;
  1383. u16 devid;
  1384. if (dev->bus != &pci_bus_type)
  1385. return;
  1386. pdev = to_pci_dev(dev);
  1387. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1388. if (devid > 0)
  1389. detach_device(domain, devid);
  1390. iommu = amd_iommu_rlookup_table[devid];
  1391. if (!iommu)
  1392. return;
  1393. iommu_queue_inv_dev_entry(iommu, devid);
  1394. iommu_completion_wait(iommu);
  1395. }
  1396. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1397. struct device *dev)
  1398. {
  1399. struct protection_domain *domain = dom->priv;
  1400. struct protection_domain *old_domain;
  1401. struct amd_iommu *iommu;
  1402. struct pci_dev *pdev;
  1403. u16 devid;
  1404. if (dev->bus != &pci_bus_type)
  1405. return -EINVAL;
  1406. pdev = to_pci_dev(dev);
  1407. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1408. if (devid >= amd_iommu_last_bdf ||
  1409. devid != amd_iommu_alias_table[devid])
  1410. return -EINVAL;
  1411. iommu = amd_iommu_rlookup_table[devid];
  1412. if (!iommu)
  1413. return -EINVAL;
  1414. old_domain = domain_for_device(devid);
  1415. if (old_domain)
  1416. return -EBUSY;
  1417. attach_device(iommu, domain, devid);
  1418. iommu_completion_wait(iommu);
  1419. return 0;
  1420. }
  1421. static int amd_iommu_map_range(struct iommu_domain *dom,
  1422. unsigned long iova, phys_addr_t paddr,
  1423. size_t size, int iommu_prot)
  1424. {
  1425. struct protection_domain *domain = dom->priv;
  1426. unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1427. int prot = 0;
  1428. int ret;
  1429. if (iommu_prot & IOMMU_READ)
  1430. prot |= IOMMU_PROT_IR;
  1431. if (iommu_prot & IOMMU_WRITE)
  1432. prot |= IOMMU_PROT_IW;
  1433. iova &= PAGE_MASK;
  1434. paddr &= PAGE_MASK;
  1435. for (i = 0; i < npages; ++i) {
  1436. ret = iommu_map_page(domain, iova, paddr, prot);
  1437. if (ret)
  1438. return ret;
  1439. iova += PAGE_SIZE;
  1440. paddr += PAGE_SIZE;
  1441. }
  1442. return 0;
  1443. }
  1444. static void amd_iommu_unmap_range(struct iommu_domain *dom,
  1445. unsigned long iova, size_t size)
  1446. {
  1447. struct protection_domain *domain = dom->priv;
  1448. unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
  1449. iova &= PAGE_MASK;
  1450. for (i = 0; i < npages; ++i) {
  1451. iommu_unmap_page(domain, iova);
  1452. iova += PAGE_SIZE;
  1453. }
  1454. iommu_flush_domain(domain->id);
  1455. }
  1456. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1457. unsigned long iova)
  1458. {
  1459. struct protection_domain *domain = dom->priv;
  1460. unsigned long offset = iova & ~PAGE_MASK;
  1461. phys_addr_t paddr;
  1462. u64 *pte;
  1463. pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)];
  1464. if (!IOMMU_PTE_PRESENT(*pte))
  1465. return 0;
  1466. pte = IOMMU_PTE_PAGE(*pte);
  1467. pte = &pte[IOMMU_PTE_L1_INDEX(iova)];
  1468. if (!IOMMU_PTE_PRESENT(*pte))
  1469. return 0;
  1470. pte = IOMMU_PTE_PAGE(*pte);
  1471. pte = &pte[IOMMU_PTE_L0_INDEX(iova)];
  1472. if (!IOMMU_PTE_PRESENT(*pte))
  1473. return 0;
  1474. paddr = *pte & IOMMU_PAGE_MASK;
  1475. paddr |= offset;
  1476. return paddr;
  1477. }
  1478. static struct iommu_ops amd_iommu_ops = {
  1479. .domain_init = amd_iommu_domain_init,
  1480. .domain_destroy = amd_iommu_domain_destroy,
  1481. .attach_dev = amd_iommu_attach_device,
  1482. .detach_dev = amd_iommu_detach_device,
  1483. .map = amd_iommu_map_range,
  1484. .unmap = amd_iommu_unmap_range,
  1485. .iova_to_phys = amd_iommu_iova_to_phys,
  1486. };
  1487. #endif