amd_iommu.c 52 KB

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