amd_iommu.c 46 KB

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