amd_iommu.c 58 KB

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