amd_iommu.c 51 KB

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