amd_iommu.c 47 KB

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